自定义宽度的indicator

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:gravity="center">
<shape android:shape="rectangle">
<size
android:width="@dimen/m20"
android:height="@dimen/m2_5" />
</shape>
</item>
</layer-list>
<com.google.android.material.tabs.TabLayout
android:id="@+id/tab_laout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/transparent"
app:tabIndicator="@drawable/indicator_circle_shape"/>
TabLayout 设置选中Tab字体加粗

protected void initViews() {
...
updateBoldTab(viewDataBinding.tabLaout.getTabAt(0), Typeface.BOLD);
viewDataBinding.tabLaout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
updateBoldTab(tab, Typeface.BOLD);
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
updateBoldTab(tab, Typeface.NORMAL);
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
private void updateBoldTab(TabLayout.Tab tab, int style) {
if (tab == null || tab.getText() == null) {
return;
}
String trim = tab.getText().toString().trim();
SpannableString spStr = new SpannableString(trim);
StyleSpan styleSpan_B = new StyleSpan(style);
spStr.setSpan(styleSpan_B, 0, trim.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
tab.setText(spStr);
}
TabLayout 设置选中Tab字体加粗(还有一种方式,就是tab布局完全自定义)

private static final String TAB1 = "我是TAB1";
private static final String TAB2 = "我是TAB2";
@Override
protected void initViews() {
......
viewDataBinding.tabLaout.addTab(viewDataBinding.tabLaout.newTab().setText(TAB1));
viewDataBinding.tabLaout.addTab(viewDataBinding.tabLaout.newTab().setText(TAB2));
initCustomTabView(0, TAB1);
initCustomTabView(1, TAB2);
updateBoldTab(viewDataBinding.tabLaout.getTabAt(0), Typeface.BOLD, R.color.common_color_2C78FF);
viewDataBinding.tabLaout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
updateBoldTab(tab, Typeface.BOLD, R.color.common_color_2C78FF);
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
updateBoldTab(tab, Typeface.NORMAL, R.color.common_color_333333);
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
private void initCustomTabView(int position, String text) {
TabLayout.Tab tab = viewDataBinding.tabLaout.getTabAt(position);
if (tab != null) {
tab.setCustomView(R.layout.main_top_item);
View customTabView = tab.getCustomView();
assert customTabView != null;
TextView tabTextView = customTabView.findViewById(R.id.tabTextView);
tabTextView.setText(text);
tab.setTag(position);
}
}
private void updateBoldTab(TabLayout.Tab tab, int style, int color) {
if (tab == null || tab.getText() == null) {
return;
}
View customTabView = tab.getCustomView();
if (customTabView != null) {
TextView tabTextView = customTabView.findViewById(R.id.tabTextView);
if (tabTextView != null) {
tabTextView.setTypeface(null, style);
tabTextView.setTextColor(ContextCompat.getColor(this, color));
}
}
}