Last active
June 7, 2018 11:32
-
-
Save Runly/75657c18c9f3f3eb8a1c9b6ae0c431cd to your computer and use it in GitHub Desktop.
change TabLayout Indicator width 修改TabLayout Indicator的宽度
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 通过反射修改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