編輯:關於 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 的使用就介紹完了。希望本文對你的學習有所幫助。
calabash-android 命令列使用教學:query、touch、wait
本文用實例帶大家熟悉 calabash-android 的命令列操作,重點放在 query、touch、wait 等常用指令背後對應的 Ruby API。把這些 API
安卓微信閃退怎麼辦?兩招搞定閃退與打不開問題
一提到 APP 閃退,許多人會先聯想到 iOS 裝置的問題,但其實閃退並非蘋果的「專利」。近期就有安卓用戶遇到微信閃退的情形,卻不知如何是好。以下就為大家提供解決手機微信
Android 自定義 View 進階:徹底搞懂 Matrix 的原理
前言 本文偏理論,與先前「畫布操作」一篇有部分重疊,但會更深入地說明背後的原理。 本篇主角 Matrix 是個一直在幕後默默工作的模範勞工。我們看到的每一個 View,背
Android AsyncTask 用法與源碼全解析
綜述 Android 中遇到耗時操作,標準做法是丟到子線程執行,完成後再通知 UI 線程更新界面。早期我們用「Thread + Handler」組合實現,但每次都要寫一堆