总的代码如下:改style放到values的style中,就重新设置了tablayout的属性了!!!
<!-- 修改tablayout的背景色,提示线的颜色,字体颜色等-->
<style name="Base.Widget.Design.TabLayout" parent="android:Widget">
<item name="tabBackground">@android:color/white</item>
<item name="tabIndicatorColor">#3D87FB</item>
<item name="tabIndicatorHeight">2dp</item>
<item name="tabTextAppearance">@style/TextAppearance.Design.Tab</item>
<item name="tabSelectedTextColor">#3D87FB</item>
</style>
<style name="TextAppearance.Design.Tab" parent="TextAppearance.AppCompat.Button">
<item name="android:textSize">15sp</item>
<item name="android:textColor">#666666</item>
<item name="textAllCaps">true</item>
</style>
1: 修改背景色
<item name="tabBackground">@android:color/white</item>
2: 字体颜色
3: 指示线
A: 颜色和高度
<item name="tabIndicatorColor">#3D87FB</item>
<item name="tabIndicatorHeight">2dp</item>
B: 指示线的宽度
其中的20,20代表了,左边间距,右边间距
mTabLayout.post(new Runnable() {
@Override
public void run() {
setIndicator(mTabLayout,20,20);
}
});
public void setIndicator(TabLayout tabs, int leftDip, int rightDip) {
Class<?> tabLayout = tabs.getClass();
Field tabStrip = null;
try {
tabStrip = tabLayout.getDeclaredField("mTabStrip");
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
tabStrip.setAccessible(true);
LinearLayout llTab = null;
try {
llTab = (LinearLayout) tabStrip.get(tabs);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
int left = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
leftDip, Resources.getSystem().getDisplayMetrics());
int right = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
rightDip, Resources.getSystem().getDisplayMetrics());
for (int i = 0; i < llTab.getChildCount(); i++) {
View child = llTab.getChildAt(i);
child.setPadding(0, 0, 0, 0);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1);
params.leftMargin = left;
params.rightMargin = right;
child.setLayoutParams(params);
child.invalidate();
}
}