引用: https://blog.csdn.net/qq_34906385/article/details/93524163
app:tabIndicatorColor :指示线的颜色
app:tabIndicatorHeight : 指示线的高度
app:tabIndicatorFullWidth="false" 指示线是否铺满宽度
app:tabSelectedTextColor : tab选中时的字体颜色
app:tabTextColor="@color/colorPrimary" :未选中字体颜色
app:tabBackground="color" : 整个tablayout颜色
app:tabMode="scrollable" : 默认是fixed,固定的;scrollable:可滚动的
//监听一定要在setupWithViewPager方法之前添加
binding.tablayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
changeTabTextView(tab, true);
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
changeTabTextView(tab, false);
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
// 选中第一个
changeTabTextView(binding.tablayout.getTabAt(0), true);
public void changeTabTextView(TabLayout.Tab tab, boolean isBold) {
View view = tab.getCustomView();
if (null == view) {
tab.setCustomView(R.layout.tab_layout_text);
}
TextView textView = tab.getCustomView().findViewById(android.R.id.text1);
if (isBold) {
textView.setTextAppearance(context, R.style.TabLayoutBoldTextSize);
} else {
textView.setTextAppearance(context, R.style.TabLayoutNormalTextSize);
}
}
17sp
bold
#fc5a44
15sp
normal
#24272E
作者:Liuyz0420