思想:运用反射的方式来实现
封装的方法如下:
public voidsetIndicator(TabLayout tabs, intleftDip, intrightDip) {
Class tabLayout = tabs.getClass();
Field tabStrip =null;
try{
tabStrip = tabLayout.getDeclaredField("mTabStrip");
}catch(NoSuchFieldException e) {
e.printStackTrace();
}
tabStrip.setAccessible(true);
LinearLayout llTab =null;
try{
llTab = (LinearLayout) tabStrip.get(tabs);
}catch(IllegalAccessException e) {
e.printStackTrace();
}
intleft = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,leftDip,Resources.getSystem().getDisplayMetrics());
intright = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,rightDip,Resources.getSystem().getDisplayMetrics());
for(inti =0;i < llTab.getChildCount();i++) {
View child = llTab.getChildAt(i);
child.setPadding(0,0,0,0);
LinearLayout.LayoutParams params =newLinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,1);
params.leftMargin= left;
params.rightMargin= right;
child.setLayoutParams(params);
child.invalidate();
}
}
在tablayout渲染完成之后调用
tabLayout.post(newRunnable() {
@Override
public voidrun() {
setIndicator(tabLayout,40,40);
}
});
网友评论