Last active
June 12, 2020 03:38
-
-
Save brucevanfdm/b3829f2135e9b07dee5f8f8c48f63cd2 to your computer and use it in GitHub Desktop.
通过反射修改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() { | |
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); | |
} | |
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(dip2px(mContext, 18f)); | |
params.setMarginEnd(dip2px(mContext, 18f)); | |
} | |
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
mTabStrip这个地方我应该写成什么