tablayout较全的用法(会陆续补充)
<android.support.design.widget.TabLayout
android:id="@+id/tablayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
//背景颜色
android:background="@color/gray_new"
app:tabGravity="fill"
//下划线颜色
app:tabIndicatorColor="@color/blue"
//下划线高度
app:tabIndicatorHeight="@dimen/pad_3"
app:tabMode="fixed"
//字体样式(下边会补充)
app:tabTextAppearance="@style/MyTabLayoutTextAppearance"
//选中时的字体颜色
app:abTextColor="#000000" />
tablayout 标题文字样式
<style name="MyTabLayoutTextAppearance" parent="TextAppearance.AppCompat.Widget.ActionBar.Title">
<item name="android:textSize">20sp</item>
<item name="android:textColor">@android:color/black</item>
<item name="android:textStyle">bold</item>
</style>
改变下划线长度(通过反射的方法)
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();
}
}
在oncreate中调用修改长度
tabLayout.post(new Runnable() {
@Override
public void run() {
setIndicator(tabLayout, 10, 10);
}
});