TabLayout设置下划线(Indicator)宽度

再战TabLayout之下划线宽度


18年12月再编辑。时间久远---通过评论区知道28版本源码已经不适用,并且官方已经通过属性支持了,推荐使用官方属性。

app:tabIndicatorFullWidth=false;

原文(28以下版本的解决方案)

这周的需求搞定之后,想到之前有一个小瑕疵,反正没什么事,索性较量较量

 

如图官方原版就是小瑕疵,反射版本就是最终

 

解决方案-Demo源码(CSDN下载的积分不是上传者控制,而是csdn根据热度控制)

百度云链接(没积分的点过来)

https://pan.baidu.com/s/1aq8rd0NNEg1OhWNjJIARpA

先讲解决方案。直接贴代码(要在tabLayout添加完所有的tab后调用)

 

 

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

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

          
  • 14
    点赞
  • 42
    收藏
    觉得还不错? 一键收藏
  • 42
    评论
要隐藏 Android TabLayout 中单个选项卡的下划线,可以在 TabLayout.Tab 上使用 `setCustomView()` 方法来设置自定义视图,在自定义视图中隐藏下划线。 例如,可以创建一个自定义布局来显示选项卡的文本和图标,同时隐藏下划线: ```java TabLayout.Tab tab = tabLayout.newTab(); View customView = LayoutInflater.from(this).inflate(R.layout.custom_tab_layout, null); TextView tabText = customView.findViewById(R.id.tab_text); ImageView tabIcon = customView.findViewById(R.id.tab_icon); tabText.setText("Tab Title"); tabIcon.setImageResource(R.drawable.tab_icon); tab.setCustomView(customView); ``` 然后,在 `custom_tab_layout.xml` 布局文件中可以隐藏下划线: ```xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:orientation="vertical"> <TextView android:id="@+id/tab_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/tab_text_color" android:textSize="@dimen/tab_text_size" /> <ImageView android:id="@+id/tab_icon" android:layout_width="@dimen/tab_icon_size" android:layout_height="@dimen/tab_icon_size" android:scaleType="centerInside" android:src="@drawable/tab_icon" /> <View android:id="@+id/tab_indicator" android:layout_width="0dp" android:layout_height="2dp" android:background="@color/transparent" /> </LinearLayout> ``` 在 `custom_tab_layout.xml` 中添加了一个名为 `tab_indicator` 的 View,并将其背景设置为透明,这样就可以隐藏下划线了。
评论 42
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值