首先自定tabIndicator,在drawable 目录下定义tabIndicator.xml,如下:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:width="20dp"
android:height="6dp"
android:gravity="center_horizontal">
<shape>
<solid android:color="#FF1DB998" /> <!-- 指示器颜色 -->
<corners android:radius="3dp" /> <!-- 指示器圆角 -->
</shape>
</item>
</layer-list>
然后在xml 布局中通过 app:tabIndicator="@drawable/tab_indicator" 设置,如下:
<com.google.android.material.tabs.TabLayout
android:id="@+id/tl_chat"
app:tabMode="scrollable"
app:tabIndicatorHeight="6dp"
app:tabIndicatorFullWidth="false"
android:background="@android:color/transparent"
app:tabIndicator="@drawable/tab_indicator"
app:tabIndicatorColor="#FF1DB998"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent">
</com.google.android.material.tabs.TabLayout>