注:使用新的属性需要设置implementation 'com.android.support:design:28.0.0'
在布局里加入 TabLayout,默认是下划线的样式,可以使用 tabIndicatorGravity 属性设置为:bottom(默认值,可以不用设置,指示器显示在底部)、 top(指示器显示在顶部)、center(指示器显示在中间)、stretch(指示器高度拉伸铺满 item)。
android:id="@+id/tl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabIndicatorColor="@color/colorPrimary"
app:tabIndicatorFullWidth="true"
app:tabIndicatorHeight="2dp"
app:tabMode="scrollable" />
1. "app:tabIndicatorFullWidth" 属性
注意 app:tabIndicatorFullWidth="true" 属性,设为 true,是 Indicator 充满 item 的宽度:
app:tabIndicatorFullWidth=