android TabLayout 相关

1. TabLayout 设置 指示条 颜色、高度,字体颜色大小等

<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:background="@color/colorAccent"
android:layout_gravity="top"
app:tabIndicatorColor="@color/colorBlue"
app:tabIndicatorHeight="10dp"
app:tabSelectedTextColor="@color/colorBlue"
app:tabTextAppearance="@style/MyTabLayoutText"
app:tabTextColor="@color/black" />

添加style
<style name="MyTabLayoutText" parent="TextAppearance.Design.Tab">
   
<item name="android:textSize">40sp</item>
</style>

2.TabLayout 添加tab方法

mTabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
mTabLayout.addTab(mTabLayout.newTab().setText(ResourcesUtil.getString(
this, R.string.txt_to_send_title)).setTag(TAG_TO_SEND));
mTabLayout.addTab(mTabLayout.newTab().setText(ResourcesUtil.getString(
this, R.string.txt_wonderful_review)).setTag(TAG_SEND_REVIEW));
mTabLayout.addOnTabSelectedListener(this);

3.TabLayout 设置子item margin值方法

public void setTabItem(@NonNull final TabLayout tabLayout, int marginRight) {
LogUtil.d(TAG + "setTabItem 000 tabLayout:" + tabLayout);
tabLayout.post(new Runnable() {
@Override
public void run() {
try {
LogUtil.d(TAG + "setTabItem 111");
//Get the mtabstrip property of tablayout.
Field tabStripField = tabLayout.getClass().getDeclaredField("mTabStrip");
tabStripField.setAccessible(true);
LinearLayout tabStrip = (LinearLayout) tabStripField.get(tabLayout);
int count = tabStrip.getChildCount();
for (int i = 0; i < count; i++) {
View tabView = tabStrip.getChildAt(i);
//Get the mtextview property of tabview.
Field textViewField = tabView.getClass().getDeclaredField("mTextView");
textViewField.setAccessible(true);
TextView textView = (TextView) textViewField.get(tabView);
tabView.setPadding(0, 0, 0, 0);
//Because I want the effect to be as wide as the line is,
// measure the width of mtextview.
int width = textView.getWidth();
if (width == 0) {
textView.measure(0, 0);
width = textView.getMeasuredWidth();
}
//Set the space between the left and right of the tab.Note that padding
// cannot be used here, because the width of the centerline in the
// source code is set according to the width of the tabview
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)
tabView.getLayoutParams();
params.width = width; //The width of the indicator bar is set here.
//params.leftMargin = margin;
LogUtil.d(TAG + "setTabItem 222 marginRight:" + marginRight);
if (i < count - 1) {
params.rightMargin = marginRight;
}
//params.bottomMargin = marginBottom;
tabView.setLayoutParams(params);
tabView.invalidate();
}
} catch (NoSuchFieldException e) {
LogUtil.d(TAG + "setTabItem 333 error:" + e);
e.printStackTrace();
} catch (IllegalAccessException e) {
LogUtil.d(TAG + "setTabItem 444 error:" + e);
e.printStackTrace();
}
}
});
}

猜你喜欢

转载自www.cnblogs.com/adamli/p/13367440.html