tablayou设置固定下划线和根据文字显示长短显示下划线

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/weixin_44144834/article/details/102721575

1、固定下划线

上图上图!!!找了蛮久的才做好,呜呜!!

在这里插入图片描述

TabLayout布局初始化

<android.support.design.widget.TabLayout
        android:id="@+id/tb_home"
        app:tabSelectedTextColor="@color/g333333"
        app:tabTextColor="@color/g999999"
       <!-- tabIndicatorHeight="0dp" 把原有的线隐藏掉-->
        app:tabIndicatorHeight="0dp"
         <!--设置选中样式-->
        app:tabBackground="@drawable/tab_select"
        android:background="@color/white"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </android.support.design.widget.TabLayout>

tab_select初始化

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/tab_selected" android:state_selected="true" />
    <item android:drawable="@color/white" />
</selector>

tab_selected初始化

<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- 整个背景色-->
    <item>
        <shape>
            <solid android:color="@color/white" />
        </shape>
    </item>
    <!-- 底部圆角,参数是固定的 -->
    <item
        android:width="24dp"
        android:height="2dp"
        android:gravity="bottom|center_horizontal">
        <shape>
            <solid android:color="@color/gffc105" />
            <corners android:radius="1dp" />
        </shape>
    </item>
</layer-list>

2、根据文字显示自适应下划线

/**
     * 通过反射机制 修改TableLayout 的下划线长度
     */
    public void reflexWith(final TabLayout tabLayout) {
        //了解源码得知 线的宽度是根据 tabView的宽度来设置的
        tabLayout.post(new Runnable() {
            @Override
            public void run() {
                try {
                    //拿到tabLayout的mTabStrip属性
                    LinearLayout mTabStrip = (LinearLayout) tabLayout.getChildAt(0);

                    int dp10 = DensityUtil.dip2px(context, 20);// dip2px(tabLayout.getContext(), 10);

                    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左右间距为10dp  注意这里不能使用Padding 因为源码中线的宽度是根据 tabView的宽度来设置的
                        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) tabView.getLayoutParams();
                        params.width = width;//宽度可调width-25

                        params.leftMargin = dp10;
                        params.rightMargin = dp10;
                        tabView.setLayoutParams(params);

                        tabView.invalidate();
                    }

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

猜你喜欢

转载自blog.csdn.net/weixin_44144834/article/details/102721575
今日推荐