设置选项卡的文本颜色

set textcolor of tab

为什么我无法设置 tablayout 的文本颜色?

这里是完整的 xml-来源。相关行是:

 app:tabTextColor="@color/tabTextColor"

文本不是白色的 - 它是黑色的

这是完整的源代码

 <?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"


 xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/linlayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <include layout="@layout/toolbar"/>

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#4f83cc"
        app:tabMode="scrollable"
        app:tabIndicatorColor="#FFFFFF"
        app:tabTextColor="@color/tabTextColor"
        >
    </android.support.design.widget.TabLayout>


    <android.support.v4.view.ViewPager

        android:id="@+id/viewPager"
        android:layout_below="@+id/tabs"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        />

并且在颜色-xml 文件中:

 <?xml version="1.0" encoding="utf-8"?>
<resources>
   <color name="colorPrimary">#01579b</color>
   <color name="colorPrimaryDark">#002f6c</color>
   <color name="colorAccent">#FF4081</color>
   <color name="tabTextColor">#FFFFFF</color>
</resources>

试试这个:

     app:tabSelectedTextColor="@color/white"
     app:tabIndicatorColor="@color/white"

如果它仍然不起作用,请以编程方式进行:https://www.whosebug.com/a/36962857

希望对您有所帮助。

您可以使用 java 代码

更改 TabLayout 的文本颜色
tabLayout.setTabTextColors(
  getResources().getColor(R.color.your_unselected_text_color),
  getResources().getColor(R.color.your_selected_text_color)
);

或者您可以尝试为 TabLayout 自定义样式

<style name="MyCustomTabLayout" parent="Widget.Design.TabLayout">
  <item name="tabTextAppearance">@style/MyCustomTabText</item>
  <item name="tabSelectedTextColor">@color/tab_text_act</item>
</style>

<style name="MyCustomTabText" parent="TextAppearance.AppCompat.Button">
  <item name="android:textSize">14sp</item>
  <item name="android:textColor">@color/tab_text</item>
</style>

这是代码

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