【Android】TabLayout 自定义指示器 Indicator 样式

app:tabIndicatorHeight=“2dp”

app:tabIndicatorColor=“@color/colorPrimary”

app:tabIndicator=“@drawable/indicator”
app:tabMode=“scrollable” />

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

3. 给 Indicator 设置圆角

如果不需要边距,只需要圆角,可以配合 app:tabIndicatorFullWidth 属性,使用 shape 设置 app:tabIndicator 来实现圆角即可,无需使用 layer-list,代码就不用贴了吧~

这里为了使效果看得明显一点,把 Indicator 的高度设置为 5dp。 给 Indicator 添加了 5dp 的圆角:

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

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

4. 给 Indicator 设置宽高

4.1 在 <shape><size> 标签里设置宽高(API 23 以上):
<?xml version="1.0" encoding="utf-8"?>
4.2 在 layer-list 里给 <item> 标签设置宽高(API 23 以上):
<?xml version="1.0" encoding="utf-8"?>

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

5. tabIndicator 属性源码分析

TabLayouttabIndicator 属性里设置的 layer-list 不支持设置颜色。 我们查看一下 TabLayout 的源码,搜索 TabLayout_tabIndicator

this.setSelectedTabIndicator(MaterialResources.getDrawable(context, a, styleable.TabLayout_tabIndicator));

setSelectedTabIndicator() 方法:

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

搜索一下使用 tabSelectedIndicator 的地方,在 SlidingTabIndicator 类里的 draw() 方法里: 第 1 处 tabSelectedIndicator

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

再看一下 selectedIndicatorHeight 是什么:

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

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

selectedIndicatorHeight 是在布局里给 TabLayout 设置的 tabIndicatorHeight 属性。

可见如果我们在布局里给 TabLayout 设置了 tabIndicatorHeight 属性,则 Indicator 高度优先取 tabIndicatorHeight 设置的高度;否则才会取咱们自定义的 drawable 里的高度。

继续,第 2 处 tabSelectedIndicator

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

黄色方框里可以发现,为什么之前在 drawable 里设置的颜色无效了,因为使用的是 TabLayout_tabIndicatorColor 属性里设置的颜色,所以 <stroke> 也无效,只保留了整体的形状样式。

6. 自定义复杂的 Indicator 样式

如果需要复杂一点的样式,比如 <stroke> 。 先写一个 tab 被选中时的样式 indicator.xml

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

还需要一个 selector.xml

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

接下来,我们要设置的是 tabBackground,也就是 tab 标签的背景,而不再是tabIndicator,所以要把 Indicator 的高度设为 0 ,不使用 tab 原生的 Indicator。

这里还要注意一下 tabRippleColor 属性,是设置点击 tab 标签时的波纹颜色,不设置的时候,默认是灰色的,文章前面的截图里有显示效果。如果想去掉这个效果,设置颜色为透明即可。

<android.support.design.widget.TabLayout
android:id=“@+id/tl”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”

app:tabBackground=“@drawable/selector”

app:tabIndicatorHeight=“0dp”
app:tabMode=“scrollable”

最后是今天给大家分享的一些独家干货:

【Android开发核心知识点笔记】

【Android思维脑图(技能树)】

【Android核心高级技术PDF文档,BAT大厂面试真题解析】

【Android高级架构视频学习资源】
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》点击传送门,即可获取!

[外链图片转存中…(img-hIkNefzN-1715156613913)]

【Android高级架构视频学习资源】
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》点击传送门,即可获取!

  • 9
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将 Android TabLayout 的指示器颜色设置为渐变色,可以执行以下步骤: 1. 定义渐变色 在 drawable 目录下创建一个 gradient.xml 文件,定义渐变色。例如: ``` <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <gradient android:startColor="@color/colorStart" android:endColor="@color/colorEnd" android:angle="0" /> </shape> ``` 其中,@color/colorStart 和 @color/colorEnd 分别指定渐变色的起始和结束颜色,android:angle 表示渐变的角度。 2. 设置 TabLayout 的指示器颜色 通过 TabLayout.setSelectedTabIndicatorColor() 方法设置 TabLayout 的指示器颜色。这里将渐变色作为指示器的颜色: ``` TabLayout tabLayout = findViewById(R.id.tabLayout); tabLayout.setSelectedTabIndicatorColor(ContextCompat.getColor(this, R.color.transparent)); GradientDrawable gradientDrawable = new GradientDrawable(); gradientDrawable.setColor(ContextCompat.getColor(this, R.color.transparent)); gradientDrawable.setGradientType(GradientDrawable.LINEAR_GRADIENT); gradientDrawable.setOrientation(GradientDrawable.Orientation.LEFT_RIGHT); gradientDrawable.setColors(new int[]{ ContextCompat.getColor(this, R.color.colorStart), ContextCompat.getColor(this, R.color.colorEnd) }); tabLayout.setSelectedTabIndicator(gradientDrawable); ``` 其中,tabLayout.setSelectedTabIndicatorColor() 方法设置指示器的颜色为透明,防止出现默认的颜色叠加在渐变色上的情况。然后创建一个 GradientDrawable 对象,设置渐变色的起始和结束颜色,以及渐变的方向。最后调用 tabLayout.setSelectedTabIndicator() 方法将渐变色作为指示器的背景。 3. 设置 TabLayout 的指示器高度 通过 TabLayout.setSelectedTabIndicatorHeight() 方法设置 TabLayout 的指示器高度。例如: ``` tabLayout.setSelectedTabIndicatorHeight(getResources().getDimensionPixelSize(R.dimen.indicator_height)); ``` 其中,R.dimen.indicator_height 是在 dimens.xml 文件中定义的指示器高度的值。 这样,就可以将 Android TabLayout 的指示器颜色设置为渐变色,并且可以设置指示器的高度。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值