Skip to content

Instantly share code, notes, and snippets.

@Runly
Last active June 7, 2018 11:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Runly/75657c18c9f3f3eb8a1c9b6ae0c431cd to your computer and use it in GitHub Desktop.
Save Runly/75657c18c9f3f3eb8a1c9b6ae0c431cd to your computer and use it in GitHub Desktop.
change TabLayout Indicator width 修改TabLayout Indicator的宽度
/**
* 通过反射修改TabLayout Indicator的宽度(仅在Android 4.2及以上生效)
*/
private void setUpIndicatorWidth(TabLayout tabLayout) {
Class<?> tabLayoutClass = tabLayout.getClass();
Field tabStrip = null;
try {
tabStrip = tabLayoutClass.getDeclaredField("mTabStrip");
tabStrip.setAccessible(true);
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
LinearLayout layout = null;
try {
if (tabStrip != null) {
layout = (LinearLayout) tabStrip.get(tabLayout);
}
if (layout != null) {
for (int i = 0; i < layout.getChildCount(); i++) {
View child = layout.getChildAt(i);
child.setPadding(0, 0, 0, 0);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
params.setMarginStart(UnitConvert.dipToPixels(this, 18));
params.setMarginEnd(UnitConvert.dipToPixels(this, 18));
}
child.setLayoutParams(params);
child.invalidate();
}
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment