1. tabLayout的使用
- 布局:
<android.support.design.widget.TabLayout
android:id="@+id/tab"
android:layout_width="match_parent"
android:layout_height="?actionBarSize">
<android.support.design.widget.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tab1" />
<android.support.design.widget.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tab2"/>
<android.support.design.widget.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tab3" />
<android.support.design.widget.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tab4" />
</android.support.design.widget.TabLayout>
- 或者使用代碼添加:
TabLayout tabLayout = findViewById(R.id.tab);
TabLayout.Tab tab = tabLayout.newTab();
tab.setText("Tab1");
//添加tab
tabLayout.addTab(tab);
//刪除tab
tabLayout.removeTab(tab);
tabLayout.removeTabAt(0);
//刪除全部tab
tabLayout.removeAllTabs();
效果圖:

- 常用屬性:
顯示模式
可滑動:app:tabMode="scrollable"
固定:app:tabMode="fixed"指示器選項
指示器高度 :app:tabIndicatorHeight="10dp"
指示器顏色 :app:tabIndicatorColor="@color/colorPrimary"文字選項
選擇的Tab的文字顏色:app:tabSelectedTextColor="#ffffff"
未選擇的Tab文字顏色:app:tabTextColor="#000000"文字樣式:
app:tabTextAppearance="@style/Base.TextAppearance.AppCompat.Large"背景設(shè)置(兩者沒什么區(qū)別)
android:background="@color/colorAccent"
app:tabBackground="@color/colorPrimary"標(biāo)簽距離
app:tabPaddingStart="10dp"
app:tabPaddingBottom="10dp"
app:tabPadding="10dp"
app:tabPaddingEnd="10dp"
app:tabPaddingTop="10dp"對齊方式
居中顯示:app:tabGravity="center"
填充:app:tabGravity="fill"
偏移:app:tabContentStart="200dp"(從左邊開始偏移距離, 必須是可滑動的模式 scrollable)標(biāo)簽寬度限制
最大寬度:app:tabMaxWidth="50dp"
最小寬度:app:tabMinWidth="100dp"
2. TabLayout + ViewPager + Fragment
示例:
tabLayout包含4個tab,每個tab對應(yīng)一個單獨的fragment
fragment代碼(布局中僅包含一個textView)
public class MainFragment extends Fragment {
TextView mTextView;
public static MainFragment newInstance(String title) {
MainFragment mainFragment = new MainFragment();
Bundle bundle = new Bundle();
bundle.putString("title", title);
mainFragment.setArguments(bundle);
return mainFragment;
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_view, container, false);
mTextView = view.findViewById(R.id.textView);
mTextView.setText((CharSequence) getArguments().get("title"));
return view;
}
}
添加viewPager的adapter
class ViewPagerAdapter extends FragmentStatePagerAdapter{
List<Fragment> mFragments;
String[] mTitles;
public ViewPagerAdapter(FragmentManager fm, List<Fragment> fragments, String[] titles) {
super(fm);
mFragments = fragments;
mTitles = titles;
}
@Override
public Fragment getItem(int position) {
return mFragments.get(position);
}
@Override
public int getCount() {
return mFragments.size();
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return mTitles[position];
}
}
activity中:
mTab = findViewById(R.id.tab);
mViewPager = findViewById(R.id.viewpager);
String[] titles = {"TAB1", "TAB2", "TAB3", "TAB4"};
Fragment fragment1 = MainFragment.newInstance(titles[0]);
Fragment fragment2 = MainFragment.newInstance(titles[1]);
Fragment fragment3 = MainFragment.newInstance(titles[2]);
Fragment fragment4 = MainFragment.newInstance(titles[3]);
List<Fragment> fragments = new ArrayList<>();
fragments.add(fragment1);
fragments.add(fragment2);
fragments.add(fragment3);
fragments.add(fragment4);
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager(), fragments, titles);
mViewPager.setAdapter(adapter);
mTab.setupWithViewPager(mViewPager);

2. 使用圖標(biāo)的tabItem
tabItem有icon屬性,可以在布局中直接設(shè)置:
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="50dp"
app:tabIndicatorHeight="0dp"
app:tabMode="fixed" >
<android.support.design.widget.TabItem
android:icon="@drawable/tab_item_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<android.support.design.widget.TabItem
android:icon="@drawable/tab_item_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<android.support.design.widget.TabItem
android:icon="@drawable/tab_item_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</android.support.design.widget.TabLayout>
但是這樣有個問題,圖標(biāo)太小,沒找到可以調(diào)整圖標(biāo)大小的辦法。顯示效果不好。
這里我們可以使用自定義view 的方式。
修改布局:去掉tabItem,改為動態(tài)添加。
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="50dp"
app:tabIndicatorHeight="0dp"
app:tabMode="fixed" />
添加自定義布局:tabitem_icon_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="match_parent">
<ImageView
android:src="@drawable/tab_item_icon"
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
圖標(biāo)資源:tab_item_icon.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/ic_home_blue_24dp" android:state_selected="true" />
<item android:drawable="@drawable/ic_home_black_24dp" />
</selector>
activity中動態(tài)添加:
private void initTab() {
//添加底部tab
for (int i = 0; i < 4; i++) {
View view = getLayoutInflater().inflate(R.layout.tabitem_icon_layout, null);
view.findViewById(R.id.icon).setBackgroundResource(R.drawable.tab_item_icon);
mTabLayout.addTab(mTabLayout.newTab().setCustomView(view));
}
}
這樣就達(dá)到了我們的效果

問題:與viewPager的關(guān)聯(lián)
如果用上面提到的方法:
mTabLayout.setupWithViewPager(mViewPager);
我們設(shè)置的自定義布局就沒有了,tabItem都變成文字了。
可以采用以下方式進(jìn)行關(guān)聯(lián):
mTabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));
mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mTabLayout));