本文不介绍TabLayout的基本使用方法,只对个性化使用做些说明。现在是凌晨,太困,所以写的不太详细,见谅。
并且联合ViewPager使用的时候
1、不自定义选项卡的布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@mipmap/aa_background"
android:orientation="vertical">
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabBackground="@drawable/tab_bg"
app:tabTextAppearance="@style/TextApppearance.Design.Tab.Custom"
app:tabMaxWidth="0dp"
app:tabGravity="fill"
app:tabMode="fixed">
</android.support.design.widget.TabLayout>
<ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
<style name="TextApppearance.Design.Tab.Custom" parent="TextAppearance.AppCompat.Button">
<item name="android:textSize">40sp</item>
<item name="android:textColor">?android:textColorPrimary</item>
<item name="textAllCaps">false</item>
</style>
mViewPager = (ViewPager) findViewById(R.id.view_pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
mTabLayout.setupWithViewPager(mViewPager);
监听:
mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { @Override public void onTabSelected(TabLayout.Tab tab) { } @Override public void onTabUnselected(TabLayout.Tab tab) { } @Override public void onTabReselected(TabLayout.Tab tab) { } }); }
public class SectionsPagerAdapter extends FragmentPagerAdapter {
private static final String TAG = "SectionsPagerAdapter";
private List<Fragment> fragments = null;
private String[] titles;
public SectionsPagerAdapter(Context context, FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return fragments.get(position);
}
@Override
public int getCount() {
if (fragments == null) {
throw new RuntimeException("请通过setFragments方法,设置fragment");
}
return fragments.size();
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
Fragment fragment = null;
try {
fragment = (Fragment) super.instantiateItem(container, position);
} catch (Exception e) {
e.printStackTrace();
}
return fragment;
}
public void setFragments(List<Fragment> fragments) {
this.fragments = fragments;
}
public void setTitles(String[] titles) {
this.titles = titles;
}
//tablayout 如果使用自定义的customview那么就不需要此处
@Override
public CharSequence getPageTitle(int position) {
return this.titles[position];
}
}
这样,就可以使用了。注意,此种方式下,选项卡字体的大小只能在app:tabTextAppearance(或者对应的java代码中)修改,所以即使你获取到对应tab下的子元素,也无法改变字体大小。
2、自定义tab的view,这种方式,可以自由的修改字体大小
首先,把pagerAdapter中的如下函数的实现注释掉
public CharSequence getPageTitle(int position)
自定义一个tab_iterm.xml的layout作为自定义的view
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<TextView
android:id="@+id/tv_tab"
android:text="sss"
android:gravity="center"
android:textSize="@dimen/tab_unselect_size"
android:textColor="@color/white"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
然后,把tab加载到Tablayout里
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
//获取每一个tab对象
TabLayout.Tab tabAt = mTabLayout.getTabAt(i);
//将每一个条目设置我们自定义的视图
tabAt.setCustomView(R.layout.tab_item);
//默认选中第一个
if (i == 0) {
// 设置第一个tab的TextView是被选择的样式
// tabAt.getCustomView().findViewById(R.id.tv_tab).setSelected(true);//第一个tab被选中
//设置选中标签的文字大小
tabAt.getCustomView().findViewById(R.id.tv_tab)).setTextSize(10);
}
//通过tab对象找到自定义视图的ID
TextView textView = (TextView) tabAt.getCustomView().findViewById(R.id.tv_tab);
textView.setText(titles[i]);//设置tab上的文字
}
另外,在监听里 动态更改文字的大小
mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
//获取textview
TextView tv_tab = (TextView) tab.getCustomView().findViewById(R.id.tv_tab);
tv_tab.setTextSize(16);
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
//获取textview
TextView tv_tab = (TextView) tab.getCustomView().findViewById(R.id.tv_tab);
tv_tab.setTextSize(10);
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
这样,就可以点击选中时,文字大小变化了。