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

本文介绍了如何自定义Android TabLayout的指示器Indicator样式,包括设置`app:tabIndicatorFullWidth`属性、添加边距、设置圆角、调整宽高,以及分析`tabIndicator`属性源码。同时,讲解了如何创建复杂的Indicator样式,如设置背景颜色、波纹效果,并推荐了MagicIndicator库。
摘要由CSDN通过智能技术生成

注:使用新的属性需要设置implementation 'com.android.support:design:28.0.0'

在布局里加入 TabLayout,默认是下划线的样式,可以使用 tabIndicatorGravity 属性设置为:bottom(默认值,可以不用设置,指示器显示在底部)、 top(指示器显示在顶部)、center(指示器显示在中间)、stretch(指示器高度拉伸铺满 item)。

<android.support.design.widget.TabLayout
    android:id="@+id/tl"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:tabIndicatorColor="@color/colorPrimary"
    app:tabIndicatorFullWidth="true"
    <!-- 设置 Indicator 高度 -->
    app:tabIndicatorHeight="2dp" 
    app:tabMode="scrollable" />

1. “app:tabIndicatorFullWidth” 属性

注意 app:tabIndicatorFullWidth="true" 属性,设为 true,是 Indicator 充满 item 的宽度:

app:tabIndicatorFullWidth="true"

设为 false 是 Indicator 保持和 item 的内容宽度一致:

app:tabIndicatorFullWidth="false"

2. 给 Indicator 设置边距

网上的做法一般是通过反射来设置 Indicator 的宽度,可以参见博客:
关于Android改变TabLayout 下划线(Indicator)宽度实践总结

不过我觉得可以使用 layer-list 来实现。
drawable 文件夹下新建一个 indicator.xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        <!-- 设置左边距 -->
        android:left="15dp"
        <!-- 设置右边距 -->
        android:right="15dp">
        <!-- 注:这里需要一个空的 <shape /> 标签,否则会报错 -->
        <shape />
    </item>
</layer-list>

需要注意的是在 shape 里设置颜色是无效的,需要在布局文件里设置 Indica

要将 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 的指示器颜色设置为渐变色,并且可以设置指示器的高度。
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值