无法在TabLayout上更改活动文本颜色

Xer*_*dea 9 android

我试图在android.support.design.widget.TabLayout中设置标签样式我无法更改选定的标签颜色,它总是设置为我的应用主题中的textColorPrimary,但我需要它们是不同的颜色.

我已尝试在styles.xml中设置适用于TabLayout的值,但我读过你不能以这种方式更改活动标签文本颜色,尽管我可以更改未选择的标签文本颜色.我也尝试过:

tabLayout.setTabTextColors(getResources().getColorStateList(R.color.selector));
Run Code Online (Sandbox Code Playgroud)

tabLayout.setTabTextColors(R.color.Green, R.color.Blue);
Run Code Online (Sandbox Code Playgroud)

是否可以覆盖选定的标签文字颜色?

Xer*_*dea 14

编辑:让它工作,

tabLayout.setTabTextColors(getResources().getColorStateList(R.color.selector));
Run Code Online (Sandbox Code Playgroud)

需要在它附加到视图寻呼机之前调用


ash*_*rov 12

实际上,您可以通过定义自定义TabLayout样式自定义活动标签文本颜色.看看tabSelectedTextColor参数.下面是示例定制的tabSelectedTextColor,tabIndicatorColor,tabTextAppearance(文本大小/颜色等):

<android.support.design.widget.TabLayout
    android:id="@+id/tabLayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    style="@style/CustomTabLayoutStyle"/>
Run Code Online (Sandbox Code Playgroud)

样式:

<style name="CustomTabLayoutStyle" parent="Base.Widget.Design.TabLayout">
    <item name="tabSelectedTextColor">@color/tab_text_selected</item>
    <item name="tabIndicatorColor">@color/tab_indicator</item>
    <item name="tabTextAppearance">@style/CustomTabTexStyle</item>
</style>

<style name="CustomTabTexStyle" parent="TextAppearance.Design.Tab">
    <item name="android:textSize">14sp</item>
    <item name="android:textColor">@color/tab_text</item>
    <item name="textAllCaps">false</item>
    ...
</style>
Run Code Online (Sandbox Code Playgroud)


vee*_*son 8

将以下代码添加到xml中:

app:tabSelectedTextColor="@color/app_color"
Run Code Online (Sandbox Code Playgroud)