xml
<android.support.design.widget.TabLayout
android:id="@+id/tl_tab"
android:layout_width="wrap_content"
android:layout_height="65px"
android:layout_gravity="center_horizontal"
app:tabGravity="center"
app:tabIndicatorColor="@color/color_FF0073cf"
app:tabTextAppearance="@style/CreatePatientTabLayoutTextStyle"
app:tabMode="scrollable"
app:tabSelectedTextColor="@color/color_FF0073cf"
app:tabTextColor="@color/color_999999" />
<android.support.v4.view.ViewPager
android:id="@+id/vp_pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
java
public void setTabPadding() {
Class tablayout = tlTab.getClass();
Field tabStrip = null;
try {
tabStrip = tablayout.getDeclaredField("mTabStrip");
tabStrip.setAccessible(true);
LinearLayout ll_tab = (LinearLayout) tabStrip.get(tlTab);
for (int i = 0; i < ll_tab.getChildCount(); i++) {
int width = 0;
switch (i){
case 0:
case 3:
case 4:
case 5:
width = 100;
break;
case 1:
case 2:
width = 80;
}
View child = ll_tab.getChildAt(i);
child.setPadding(0, 0, 0, 0);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width, LinearLayout.LayoutParams.MATCH_PARENT);
child.setLayoutParams(params);
child.invalidate(); // 这个方法是重画
}
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}