关于Android改变TabLayout 下划线(Indicator)宽度实践总结

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

}

效果图如下:

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

提醒:这种方式改变Indicator最短也就Tab内容的宽度,如果设置很短,Tab内容就显示不下,如下图:

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

二、通过TabLayout setCustomView 的方式

第一种通过反射的方式设置Indicator宽度,最短只能Tab内容的宽度,如果设计师要所有选中的Tab下的Indicator都设置一个指定的宽度,这种就不行了。TabLayout可以设置自定义View,可以通过这种方法来达到目的。

1, 将TabLayout 的tabIndicatorHeight 设置为0
2,通过TabLayout 的setCustomView方式添加Tab
3, 在onTabSelected 回调种,处理Tab选中和未选中的状态;
4,为了方便使用,封装成一个通用的View

首先看布局: enhance_tab_layout.xml:

<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.TabLayout
android:id=“@+id/enhance_tab_view”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
app:tabIndicatorHeight=“0dp”

</android.support.design.widget.TabLayout>

Tab item 布局:tab_item_layout.xml

<?xml version="1.0" encoding="utf-8"?>



如上,TextView显示Tab内容,下面的View就是Tab下面的Indicator(下划线)。 自己定义的View,宽度随便你改。

添加Tab的时候使用setCustomView 方法:

/**

  • 添加tab
  • @param tab
    */
    public void addTab(String tab){
    mTabList.add(tab);
    View customView = getTabView(getContext(),tab,mIndicatorWidth,mIndicatorHeight,mTabTextSize);
    mCustomViewList.add(customView);
    mTabLayout.addTab(mTabLayout.newTab().setCustomView(customView));
    }

/**

  • 获取Tab 显示的内容
  • @param context
  • @param
  • @return
    */
    public static View getTabView(Context context,String text,int indicatorWidth,int indicatorHeight,int textSize) {
    View view = LayoutInflater.from(context).inflate(R.layout.tab_item_layout, null);
    TextView tabText = (TextView) view.findViewById(R.id.tab_item_text);
    if(indicatorWidth>0){
    View indicator = view.findViewById(R.id.tab_item_indicator);
    ViewGroup.LayoutParams layoutParams = indicator.getLayoutParams();
    layoutParams.width = indicatorWidth;
    layoutParams.height = indicatorHeight;
    indicator.setLayoutParams(layoutParams);
    }
    tabText.setTextSize(textSize);
    tabText.setText(text);
    return view;
    }

然后在onTabSelected中处理状态:

@Override
public void onTabSelected(TabLayout.Tab tab) {
mViewPager.setCurrentItem(tab.getPosition());
EnhanceTabLayout mTabLayout = mTabLayoutRef.get();
if(mTabLayoutRef!=null){
List customViewList = mTabLayout.getCustomViewList();
if(customViewList == null || customViewList.size() ==0){
return;
}
for (int i=0;i<customViewList.size();i++){
View view = customViewList.get(i);
if(view == null){
return;
}
TextView text = (TextView) view.findViewById(R.id.tab_item_text);

  • 23
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要隐藏 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,并将其背景设置为透明,这样就可以隐藏下划线了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值