Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Material Design TabLayout 導航欄使用詳解

編輯:關於 Android 程式設計

TabLayout 的用法其實很簡單,先看效果圖:

平常項目裡要做這樣的效果,通常得靠 ViewPagerIndicator 之類的開源框架,或者自定義 HorizontalScrollView 配合 ViewPager + Fragment。自從 Google 推出 Material Design 之後,直接用 TabLayout 就能搞定。更棒的是,TabLayout 還支持自定義 View,讓導航欄的樣式更加靈活多變。

第一步,先引入 design 包,在 app 的 build.gradle 中加上依賴:

 dependencies {
 compile 'com.android.support:design:25.0.1'
 } 

接著就可以挽起袖子開幹了。

先在布局文件裡寫好結構:

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
 xmlns:tools="https://schemas.android.com/tools"
 android:id="@+id/activity_main"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical">
 <android.support.design.widget.TabLayout
 android:id="@+id/tabLayout"
 android:layout_width="match_parent"
 android:layout_height="wrap_content" />
 <android.support.v4.view.ViewPager
 android:id="@+id/viewPager"
 android:layout_width="match_parent"
 android:layout_height="0dp"
 android:layout_weight="1" />
 </LinearLayout> 

既然用到了 Fragment,就順手加個簡單的 item 布局:

 <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical">
 <TextView
 android:id="@+id/textView"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerInParent="true"
 android:text="默認" />
 </RelativeLayout> 

然后是 Fragment 與 FragmentPagerAdapter 的代碼,寫過的人應該很熟悉,直接貼:

 public class FramentAdapter extends FragmentPagerAdapter {
 private String[] titles;
 public FramentAdapter(FragmentManager fm, String[] titles) {
 super(fm);
 this.titles = titles;
 }
 @Override
 public Fragment getItem(int position) {
 return PageFragment.newInstace(position,titles);
 }
 @Override
 public int getCount() {
 return titles.length;
 }
 @Override
 public CharSequence getPageTitle(int position) {
 return titles[position];
 }
 } 

Fragment 的代碼:

 public class PageFragment extends Fragment {
 private int position;
 private String[] titles;
 private Context context;
 public static PageFragment newInstace(int position, String[] titles) {
 Bundle bundle = new Bundle();
 bundle.putInt("POSITION", position);
 bundle.putStringArray("ARRAY", titles);
 PageFragment pageFragment = new PageFragment();
 pageFragment.setArguments(bundle);
 return pageFragment;
 }
 @Override
 public void onAttach(Context context) {
 super.onAttach(context);
 this.context=context;
 }
 @Override
 public void onCreate(@Nullable Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 position = getArguments().getInt("POSITION");
 titles = getArguments().getStringArray("ARRAY");
 }
 @Nullable
 @Override
 public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
 View view =inflater.inflate(R.layout.item_layout,null);
 TextView textView = (TextView) view.findViewById(R.id.textView);
 textView.setText(titles[position]);
 return view;
 }
 } 

最後在 MainActivity 裡把它們串起來:

 public class MainActivity extends FragmentActivity {
 private TabLayout tabLayout;
 private ViewPager viewPager;
 private String[] titles = {"黃蓉", "郭靖", "楊過", "小龍女", "尹志平", "金輪法王", "收到貨就收到貨聖誕節"};
 private TextView textView;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 tabLayout = (TabLayout) findViewById(R.id.tabLayout);
 viewPager = (ViewPager) findViewById(R.id.viewPager);
 FramentAdapter framentAdapter = new FramentAdapter(getSupportFragmentManager(), titles);
 viewPager.setAdapter(framentAdapter);
 tabLayout.setupWithViewPager(viewPager);
 tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
 }
 } 

要特別注意:viewPager.setAdapter(framentAdapter); 這行必須寫在 tabLayout.setupWithViewPager(viewPager); 之前,感興趣的可以翻翻源碼。

另外,tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE); 設置了 TabLayout 的模式。mode 有兩種,這種模式下,當內容較多時可以讓 tab 平鋪滑動。

很多時候我們需要自定義樣式或自定義 tab。

自定義樣式

在 style 文件中添加自定義樣式,然後應用即可,例如:

 <style name="MyCustomTabLayout" parent="Widget.Design.TabLayout">
 <item name="tabIndicatorColor">?attr/colorAccent</item>
 <item name="tabIndicatorHeight">2dp</item>
 <item name="tabPaddingStart">12dp</item>
 <item name="tabPaddingEnd">12dp</item>
 <item name="tabBackground">?attr/selectableItemBackground</item>
 <item name="tabTextAppearance">@style/MyCustomTabTextAppearance</item>
 <item name="tabSelectedTextColor">?android:textColorPrimary</item>
 </style>
 <style name="MyCustomTabTextAppearance" parent="TextAppearance.Design.Tab">
 <item name="android:textSize">14sp</item>
 <item name="android:textColor">?android:textColorSecondary</item>
 <item name="textAllCaps">true</item>
 </style> 

另一種方式是添加自定義 View。先寫好要用的布局文件:

 <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:orientation="horizontal">
 <TextView
 android:id="@id/textView"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:gravity="center" />
 <TextView
 android:layout_width="10dp"
 android:layout_height="10dp"
 android:background="@drawable/bg_text" />
 </LinearLayout> 

接著稍微改一下 FragmentPagerAdapter,讓 getPageTitle 返回 null:

 public class FramentAdapter extends FragmentPagerAdapter {
 private String[] titles;
 public FramentAdapter(FragmentManager fm, String[] titles) {
 super(fm);
 this.titles = titles;
 }
 @Override
 public Fragment getItem(int position) {
 return PageFragment.newInstace(position,titles);
 }
 @Override
 public int getCount() {
 return titles.length;
 }
 @Override
 public CharSequence getPageTitle(int position) {
 return null;
 }
 } 

最後看看 MainActivity 裡怎麼用:

 public class MainActivity extends FragmentActivity {
 private TabLayout tabLayout;
 private ViewPager viewPager;
 private String[] titles = {"黃蓉", "郭靖", "楊過", "小龍女", "尹志平", "金輪法王", "收到貨就收到貨聖誕節"};
 private TextView textView;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 tabLayout = (TabLayout) findViewById(R.id.tabLayout);
 viewPager = (ViewPager) findViewById(R.id.viewPager);
 FramentAdapter framentAdapter = new FramentAdapter(getSupportFragmentManager(), titles);
 viewPager.setAdapter(framentAdapter);
 tabLayout.setupWithViewPager(viewPager);
 tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
 for (int i = 0; i < tabLayout.getTabCount(); i++) {
 TabLayout.Tab tab = tabLayout.getTabAt(i);
 tab.setCustomView(getTabView(i));
 }
 }
 private View getTabView(int position) {
 View view = View.inflate(this, R.layout.item_tab_view, null);
 textView = (TextView) view.findViewById(R.id.textView);
 textView.setText(titles[position]);
 return view;
 }
 } 

至此,TabLayout 的使用就介紹完了。希望本文對你的學習有所幫助。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved