Skip to content

Instantly share code, notes, and snippets.

@brucevanfdm
Last active June 12, 2020 03:38
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brucevanfdm/b3829f2135e9b07dee5f8f8c48f63cd2 to your computer and use it in GitHub Desktop.
Save brucevanfdm/b3829f2135e9b07dee5f8f8c48f63cd2 to your computer and use it in GitHub Desktop.
通过反射修改TabLayout Indicator的宽度
/**
* 通过反射修改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();
}
}
@o0o0oo00
Copy link

mTabStrip这个地方我应该写成什么

@may7be
Copy link

may7be commented Aug 6, 2018

这个基本满足不了需求嘛,属于掩耳盗铃。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment