TabLayout下划线与文本长度相等

从网上搜到的实现:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //官方原版
        TabLayout tabOfficial= (TabLayout) findViewById(R.id.tl_official);
        addTabs(tabOfficial);

        //反射
        TabLayout tabReflex= (TabLayout) findViewById(R.id.tl_reflex);
        addTabs(tabReflex);
        //添加完所有tab后调用!!
        reflex(tabReflex );

    }

    public void reflex(final TabLayout tabLayout){
        //了解源码得知 线的宽度是根据 tabView的宽度来设置的
        tabLayout.post(new Runnable() {
            @Override
            public void run() {
                try {
                    //拿到tabLayout的mTabStrip属性
                    Field mTabStripField = tabLayout.getClass().getDeclaredField("mTabStrip");
                    mTabStripField.setAccessible(true);

                    LinearLayout mTabStrip = (LinearLayout) mTabStripField.get(tabLayout);

                    int dp10 = dip2px(tabLayout.getContext(), 10);

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

                        //拿到tabView的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 ;
                        params.leftMargin = dp10;
                        params.rightMargin = dp10;
                        tabView.setLayoutParams(params);

                        tabView.invalidate();
                    }

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

    }


    public static int dip2px(Context context, float dipValue) {

        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }

    public void addTabs(TabLayout tabLayout){
        tabLayout.addTab(tabLayout.newTab().setText("全部分类"));
        tabLayout.addTab(tabLayout.newTab().setText("水果"));
        tabLayout.addTab(tabLayout.newTab().setText("茶水饮料"));
        tabLayout.addTab(tabLayout.newTab().setText("肉类"));
        tabLayout.addTab(tabLayout.newTab().setText("有机蔬菜"));
        tabLayout.addTab(tabLayout.newTab().setText("推荐"));
        tabLayout.addTab(tabLayout.newTab().setText("膨化食品"));
    }

}

布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:gravity="center_vertical"
        android:layout_height="40dp">

        <TextView
            android:layout_width="wrap_content"
            android:textColor="#17c73d"
            android:text="官方原版"
            android:layout_marginLeft="12dp"
            android:layout_marginRight="12dp"
            android:textSize="15sp"
            android:layout_height="wrap_content" />

        <android.support.design.widget.TabLayout
            android:id="@+id/tl_official"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#fff"
            app:tabGravity="fill"
            app:tabIndicatorColor="#17c73d"
            app:tabIndicatorHeight="2dp"
            app:tabMode="scrollable"
            app:tabSelectedTextColor="#17c73d"
            app:tabTextColor="#333333" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:gravity="center_vertical"
        android:layout_height="40dp">

        <TextView
            android:layout_width="wrap_content"
            android:textColor="#17c73d"
            android:text="反射版本"
            android:layout_marginLeft="12dp"
            android:layout_marginRight="12dp"
            android:textSize="15sp"
            android:layout_height="wrap_content" />

        <android.support.design.widget.TabLayout
            android:id="@+id/tl_reflex"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#fff"
            app:tabGravity="fill"
            app:tabIndicatorColor="#17c73d"
            app:tabIndicatorHeight="2dp"
            app:tabMode="scrollable"
            app:tabSelectedTextColor="#17c73d"
            app:tabTextColor="#333333" />

    </LinearLayout>

</LinearLayout>

应用到项目中的时候,反射方法获取mTabStrip属性的时候报错,查了一下原因是编译应用的时候把相应的类混淆了,修改了一下混淆文件,代码运行成功:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值