工具栏选择选项卡文本颜色android



我正在尝试在我的Android应用程序之一中为Tab文本设置自定义颜色,而是为其更改其设置的白色。其他选项卡文本正在更改,但不仅更改了选定的选项卡。

我的标签样式就像下面

<style name="MineCustomTabText"
        parent="TextAppearance.Design.Tab">
       <item name="tabSelectedTextColor">#000</item>
       <item name="android:textColor">@color/TextColorLite</item>
        <item name="android:textSize">@dimen/textPageCount</item>
    </style>

我在我的布局xml中使用它,就像以下

<android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            app:tabGravity="fill"
            app:tabTextAppearance="@style/MineCustomTabText"
            app:tabMode="fixed"
            android:layout_height="wrap_content" />

您可以看到我已经为选定选项卡的样式设置了黑色,但仅显示白色。让我知道我缺少什么。谢谢

尝试以下代码:

将此样式添加到您的TabLayout

<android.support.design.widget.TabLayout
  style="@style/MyCustomTabLayout"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"/>

将此样式添加到您的 style.xml

 <style name="MyCustomTabLayout" parent="Widget.Design.TabLayout">
        <!--<item name="tabMaxWidth">@dimen/tab_max_width</item>-->
        <item name="tabIndicatorColor">@color/appcolor</item>
        <item name="tabIndicatorHeight">2dp</item>
        <item name="tabPaddingStart">10dp</item>
        <item name="tabPaddingEnd">10dp</item>
        <item name="tabBackground">@color/lightblue</item>
        <item name="tabTextAppearance">@style/MyCustomTabTextAppearance</item>
        <item name="tabSelectedTextColor">@color/appcolor</item>
    </style>

    <style name="MyCustomTabTextAppearance" parent="TextAppearance.Design.Tab">
        <item name="android:textSize">15sp</item>
        <item name="android:textColor">@color/black</item>
        <item name="textAllCaps">true</item>
    </style>

通过编程更改的另一种方法:

 tabLayout.setSelectedTabIndicatorColor(Color.parseColor("#FF0000"));
    tabLayout.setSelectedTabIndicatorHeight((int) (5 * getResources().getDisplayMetrics().density));
    tabLayout.setTabTextColors(Color.parseColor("#727272"), Color.parseColor("#ffffff"));

我希望这对您有帮助

在可绘制的文件夹中添加 tab_text_color.xml,如下

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_selected="true"
        android:color="@color/text_tab_selected" />
    <item
        android:state_selected="false"
        android:color="@color/text_tab_unselected" />
</selector>

然后以style.xml执行此操作并检查

<style name="CustomTabPageIndicator.Text" parent="android:TextAppearance.Medium">
    <item name="android:textColor">@drawable/tab_text_color</item>
    ...
</style>