我是靠谱客的博主 传统跳跳糖,最近开发中收集的这篇文章主要介绍tablayout 添加中间竖线和下划线tablayout 添加中间竖线和下划线,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

tablayout 添加中间竖线和下划线

添加中心竖线核心代码

LinearLayout linearLayout = (LinearLayout) mTabLayout.getChildAt(0);
        linearLayout.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
        linearLayout.setDividerDrawable(ContextCompat.getDrawable(getContext(),
                R.drawable.layout_divider_vertical));

layout_divider_vertical.xml 文件中设置自己想要的样式就行了

 linearLayout.setDividerPadding(20);

设置中心属性的高度
下划线网上流行的方法有一种是利用发射机制,核心方法如下
public static void setTabWidth(final TabLayout tabLayout, final int padding){
tabLayout.post(new Runnable() {
@Override
public void run() {
try {
//拿到tabLayout的mTabStrip属性
LinearLayout mTabStrip = (LinearLayout) tabLayout.getChildAt(0);

                for (int i = 0; i < mTabStrip.getChildCount(); i++) {
                    View tabView = mTabStrip.getChildAt(i);

                    //拿到tabView的mTextView属性  tab的字数不固定一定用反射取mTextView
                    Field mTextViewField = tabView.getClass().getDeclaredField("mTextView");
                    mTextViewField.setAccessible(true);

                    TextView mTextView = (TextView) mTextViewField.get(tabView);

                    tabView.setPadding(0, 0, 0, 0);

                    //因为我想要的效果是   字多宽线就多宽,所以测量mTextView的宽度
                    int width = 0;
                    width = mTextView.getWidth();
                    if (width == 0) {
                        mTextView.measure(0, 0);
                        width = mTextView.getMeasuredWidth();
                    }

                    //设置tab左右间距 注意这里不能使用Padding 因为源码中线的宽度是根据 tabView的宽度来设置的
                    LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) tabView.getLayoutParams();
                    params.width = width ;
                    params.leftMargin = padding;
                    params.rightMargin = padding;
                    tabView.setLayoutParams(params);

                    tabView.invalidate();
                }

            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    });

}

有需求的小伙伴参看原文链接:https://www.jianshu.com/p/83922d08250b

但是我调试过程却是遇到了一下问题,可能是因为我是动态加载TabLayout中的标题名称,但是参考公司前任代码,直接修改xml中tablayout中的布局方法更加简单,适合我哈哈

在这直接根据这三个属性可以简单设置一个基础的TabLayout下划线的高度和颜色,如果项目对于下划线的要求比较高的话,可以参考上面那个前辈的链接,直接从TabLayout的源码出发,进行修改。

最后

以上就是传统跳跳糖为你收集整理的tablayout 添加中间竖线和下划线tablayout 添加中间竖线和下划线的全部内容,希望文章能够帮你解决tablayout 添加中间竖线和下划线tablayout 添加中间竖线和下划线所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(49)

评论列表共有 0 条评论

立即
投稿
返回
顶部