Android TableLayout下划线宽度
2018-08-11 本文已影响4人
淡淡_孩子气
- 直接上代码(可直接复制使用)
/**
* 设置tabLayout下划线左右边距
*
* @param tabs TabLayout
* @param leftDip 左边距
* @param rightDip 右边距
*/
public void setIndicator(TabLayout tabs, int leftDip, int rightDip) {
Class<?> tabLayout = tabs.getClass();
Field tabStrip = null;
try {
tabStrip = tabLayout.getDeclaredField("mTabStrip");
tabStrip.setAccessible(true);
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
LinearLayout llTab = null;
try {
if (tabStrip != null)
llTab = (LinearLayout) tabStrip.get(tabs);
//边距值,转换为dip。
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());
if (llTab != null) {
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();
}
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}