TabLayout的指示器的高度、颜色都可以设置,但是宽度需要自己来实现
在TabLayout view初始化后,设置
/** * @date:2017/11/9/009 * @author:平sir * @desc: 设置tablayout的指示器的宽度 */ private void initView() { mTabLayout.post(new Runnable() { @Override public void run() { setIndicator(mTabLayout, 40, 40); } }); }具体的设置方法为
/** * @date:2017/11/10/010 * @author:平sir * @desc: 设置tab指示器的宽度 */ public void setIndicator(TabLayout tabs, int leftDip, int rightDip) { 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(); } int left = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, leftDip, Resources.getSystem().getDisplayMetrics()); int right = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, rightDip, Resources.getSystem().getDisplayMetrics()); for (int i = 0; i < llTab.getChildCount(); i++) { View child = llTab.getChildAt(i); child.setPadding(0, 0, 0, 0); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1); params.leftMargin = left; params.rightMargin = right; child.setLayoutParams(params); child.invalidate(); } }
这样我们就能设置指示器的宽度了