自定义TabLayout的下划线的长度

              tablayout自定义导航线的长度的问题,我见网上有两个解决方案,我用到我的项目中都没效果,自己总结了一个方案:

             一:如果你的项目中没有滑动的动画需求你可以自定义tablayout'的item,自然而然你的导航线的长度你也可以定义;


            二:你还想要动画效果,又想自已定义导航线的长度,网上推荐的是使用反射,在我的项目中没有效果

/**
 * 对tabLayout的指示器的长度的处理
 * @param tabLayout
 * @param marginOffset
 */
public void reduceMarginsInTabs(TabLayout tabLayout, int marginOffset) {

    View tabStrip = tabLayout.getChildAt(0);
    if (tabStrip instanceof ViewGroup) {
        ViewGroup tabStripGroup = (ViewGroup) tabStrip;
        for (int i = 0; i < ((ViewGroup) tabStrip).getChildCount(); i++) {
            View tabView = tabStripGroup.getChildAt(i);
            if (tabView.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
                ((ViewGroup.MarginLayoutParams) tabView.getLayoutParams()).leftMargin = marginOffset;
                ((ViewGroup.MarginLayoutParams) tabView.getLayoutParams()).rightMargin = marginOffset;
            }
        }

        tabLayout.requestLayout();
    }
}
这样设置不知道你的有没有效果,我的这样还是没效果,如果没有效果你加上这么一句话

tabLayout.setTabsFromPagerAdapter(adapter);
这样应该可以解决;

看一下我的效果

自定义TabLayout的下划线的长度