Android Material風(fēng)格的應(yīng)用(一)--AppBar TabLayout

打造Material風(fēng)格的Android應(yīng)用

Android Material風(fēng)格的應(yīng)用(一)--AppBar TabLayout
Android Material風(fēng)格的應(yīng)用(二)--RecyclerView
Android Material風(fēng)格的應(yīng)用(三)--DrawerLayout
Android Material風(fēng)格的應(yīng)用(四)--FloatActionButton
Android Material風(fēng)格的應(yīng)用(五)--CollapsingToolbar

開發(fā)環(huán)境:

Themes And Colors

主題顏色值 res/values/colors.xml

<resources>
  <color name="colorPrimary">#3F51B5</color>
  <color name="colorPrimaryDark">#303F9F</color>
  <color name="colorAccent">#FF4081</color>
</resources>

如此,添加一個(gè)Light主題風(fēng)格的style res/values/styles.xml

<style name="AppThemeBase" parent="Theme.AppCompat.Light.NoActionBar">
  <!-- Customize your theme here. -->
  <item name="colorPrimary">@color/colorPrimary</item>
  <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
  <item name="colorAccent">@color/colorAccent</item>
</style>

使用 parent="Theme.AppCompat.Light.NoActionBar" 是在應(yīng)用中去除ActionBar運(yùn)行效果圖

布局和動(dòng)畫

TabLayout
  • 添加 Toolbar
    res/layout/activity_main.xmlMainActivity.java中添加toolbar,這里使用CoordinatorLayout作為一個(gè)容器
    里面會(huì)包含TabLayout FloatingActionButton Toolbar等,引入design的支持庫
compile 'com.android.support:design:24.2.1'

activity_main.xml

<android.support.design.widget.CoordinatorLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:id="@+id/main_content">

  <android.support.design.widget.AppBarLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:id="@+id/app_bar_layout"
      android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
      <android.support.v7.widget.Toolbar
          android:layout_width="match_parent"
          android:layout_height="?attr/actionBarSize"
          android:id="@+id/toolbar"
          app:layout_scrollFlags="enterAlways|scroll"
          app:popupTheme="@style/ThemeOverlay.AppCompat.Light"></android.support.v7.widget.Toolbar>

      <android.support.design.widget.TabLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:id="@+id/tabs">
       </android.support.design.widget.TabLayout>
      </TableLayout>
  </android.support.design.widget.AppBarLayout>

</android.support.design.widget.CoordinatorLayout>

MainActivity.java

Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
  • 添加TabLayout
TabLayout tabLayout = (TabLayout)findViewById(R.id.tabs);
tabLayout.addTab(tabLayout.newTab().setText("Tab 1"));
tabLayout.addTab(tabLayout.newTab().setText("Tab 2"));
tabLayout.addTab(tabLayout.newTab().setText("Tab 3"));
  • 添加 Fragment 和 ViewPager
    activity_main.xml中添加ViewPager
<android.support.v4.view.ViewPager
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:id="@+id/viewpager"
  app:layout_behavior="@string/appbar_scrolling_view_behavior">
</android.support.v4.view.ViewPager>

通過添加ViewPager和TabLayout聯(lián)動(dòng),創(chuàng)建3個(gè)Fragment的類
ListContentFragment.java TileContentFragment.javaCardContentFragment.java

添加item_list.xml item_tile.xml item_card.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical" android:layout_width="match_parent"
  android:layout_height="match_parent">
  <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="List"/>
</LinearLayout>

分別創(chuàng)建不同的Fragment文件和布局對(duì)應(yīng) ListContentFragment.java

public class ListContentFragment extends Fragment{
  @Nullable
  @Override
  public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
      View v = inflater.inflate(R.layout.item_list,null);
      return v;
  }
}

TileContentFragment.java

public class TileContentFragment extends Fragment{
  @Nullable
  @Override
  public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
      return inflater.inflate(R.layout.item_tile,null);
  }
}

CardContentFragment.java

public class CardContentFragment extends Fragment{
  @Nullable
  @Override
  public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
      return inflater.inflate(R.layout.item_card,null);
  }
}

MainActivity.java中添加ViewPager

ViewPager viewPager = (ViewPager)findViewById(R.id.viewpager);
setupViewPager(viewPager);

private void setupViewPager(ViewPager viewPager){
      Adapter adapter = new Adapter(getSupportFragmentManager());
      adapter.addFragment(new ListContentFragment(),"List");
      adapter.addFragment(new TileContentFragment(),"Tile");
      adapter.addFragment(new CardContentFragment(),"Card");
      viewPager.setAdapter(adapter);
  }

static class Adapter extends FragmentPagerAdapter{
    private final List<Fragment> fragmentList = new ArrayList<>();
    private final List<String> fragmentTitle = new ArrayList<>();
    public Adapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        return fragmentList.get(position);
    }

    @Override
    public int getCount() {
        return fragmentList.size();
    }

    public void addFragment(Fragment fragment,String title){
        fragmentList.add(fragment);
        fragmentTitle.add(title);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return fragmentTitle.get(position);
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,765評(píng)論 25 709
  • afinalAfinal是一個(gè)android的ioc,orm框架 https://github.com/yangf...
    passiontim閱讀 15,837評(píng)論 2 45
  • 最后一張 復(fù)古小春日 手機(jī)P圖技巧 vsco + interphoto印象 的后期
    秋回之閱讀 436評(píng)論 2 5
  • 每逢佳節(jié)倍思親啊。這是我去重慶讀大學(xué)過的第一個(gè)節(jié),戀家的我沒有被這漫長的路程所阻隔,毅然的拖著箱子踏上了回家之路。...
    z一想你就笑閱讀 374評(píng)論 0 0
  • 背景 項(xiàng)目開發(fā)中,有時(shí)候會(huì)遇到一些輸入要做特殊限制或處理,比如禁止輸入中文,禁止粘貼等,本來我們可以通過系統(tǒng)方法設(shè)...
    大猿媛閱讀 1,064評(píng)論 0 2

友情鏈接更多精彩內(nèi)容