常用属性
app:tabIndicatorColor="@color/colorPrimary_pink"//指示器的颜色
app:tabTextColor="@color/colorPrimary_pink"//tab的文字颜色
app:tabSelectedTextColor="@color/colorPrimary_pinkDark"//选中的tab的文字颜色
app:tabMode="fixed"//scrollable:可滑动;fixed:不能滑动,平分tabLayout宽度
app:tabGravity="center"// fill:tabs平均填充整个宽度;center:tab居中显示
常用方法
//1.TabLayout和Viewpager关联
tabLayout.setOnTabSelectedListener(new OnTabSelectedListener() {
@Override
public void onTabUnselected(Tab arg0) {
// TODO Auto-generated method stub
}
@Override
public void onTabSelected(Tab tab) {
// 被选中的时候回调
viewPager.setCurrentItem(tab.getPosition(),true);
}
@Override
public void onTabReselected(Tab arg0) {
// TODO Auto-generated method stub
}
});
//2.ViewPager滑动关联tabLayout
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
//设置tabLayout的标签来自于PagerAdapter
tabLayout.setTabsFromPagerAdapter(adapter);
viewPager.setAdapter(adapter);
//方法二:一步到位
tabLayout.setupWithViewPager(viewPager);
自定义TabLayout子布局
for (int i = 0; i < tabLayout.getTabCount(); i++) {
Tab tab = tabLayout.getTabAt(i);
// tab.setText(Html.toHtml(text))
View view = View.inflate(this, R.layout.bottom_navigation, null);
TextView tv_name = (TextView) view.findViewById(R.id.tv_name);
tv_name.setText(title[i]);
tab.setCustomView(view);
// tab.setIcon(icon)
}
网友评论