使用app:tabTextColor
来实现此功能。
当未选中时,它将显示app:tabTextColor
当选中时,它将显示app:tabSelectedTextColor
请使用以下代码:
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
app:tabGravity="fill"
app:tabMode="fixed"
android:background="@color/colorPrimary"
app:tabIndicatorColor="@color/white"
app:tabIndicatorHeight="4dp"
app:tabSelectedTextColor="@color/white"
app:tabTextColor="#AAAAAA"
app:tabTextAppearance="@style/NavigationTabTextAppeareance"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="4dp"
>
styles.xml : 用于加粗标签文本
<style name="NavigationTabTextAppeareance" parent="TextAppearance.Design.Tab">
<item name="android:textStyle">bold</item>
</style>
对于未选择的标签底部颜色:
您可以使用 tabtextColor 来设置默认颜色,当选项卡未被选中时。
在 drawable 中创建名为 underlinedrawable.xml 的 xml 文件。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- UNSELECTED TAB STATE -->
<item android:state_selected="false" android:state_pressed="false">
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Bottom indicator color for the UNSELECTED tab state -->
<item android:top="-5dp" android:left="-5dp" android:right="-5dp">
<shape android:shape="rectangle">
<stroke android:color="#65acee" android:width="2dp"/>
</shape>
</item>
</layer-list>
</item>
</selector>
要更改选项卡文本颜色下划线,请在主xml中添加underlinedrawable
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
app:tabGravity="fill"
app:tabMode="fixed"
app:tabTextColor="@color/unselected_color"
app:tabSelectedTextColor="@color/selected_color"
app:tabBackground="@drawable/underlinedrawable" />