如何更改选项卡布局的文本颜色?

25

我有一个改变选项卡布局文本颜色的代码,但它根本不起作用!

app:tabTextColor 不起作用,它无法将颜色更改为白色。

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white"
        app:tabIndicatorColor="@color/blue"
        app:tabIndicatorHeight="5dp"
        app:tabTextColor="@color/white" />

你的背景是白色,而你想让选项卡文本颜色也是白色。 - Bek
你想要解释什么?你能上传你的截图吗? - Bek
4个回答

94

试试看 -

<android.support.design.widget.TabLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabMode="fixed"
        app:tabGravity="fill"
        android:background="@color/colorWhite"
        app:tabTextColor="@color/colorBlack"
        app:tabSelectedTextColor="@color/colorPrimary"/>

另外,如果您想要多个可供选择的主题,请将<item name="tabTextColor">@color/...</item>放入您在Tablayout上设置的样式中(在布局xml文件中像这样设置样式:style="?attr_tab_style")。 - Squareoot

12

使用此代码可帮助在所有API级别的API 18到API 26中运行。

tabLayout.setupWithViewPager(viewPager,true);
        tabLayout.setSelected(true);

        tabLayout.setTabTextColors(getResources().getColor(R.color.colorHintTextLight),
                  getResources().getColor(R.color.colorPrimaryTextLight));

<color name="colorHintTextLight">#80FFFFFF</color>
    <color name="colorPrimaryTextLight">#FFFFFF</color>

那会对你有所帮助。当选项卡布局改变位置时,它也会对我有帮助。


不错!快速简便。 - Someone Somewhere

10
您可以自定义TabLayout的文本。
像这样从Java代码或XML创建TextView。
<?xml version="1.0" encoding="utf-8"?>
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:textSize="15sp"
    android:textColor="@color/tabs_default_color"
    android:gravity="center"
    android:layout_height="match_parent"
/>

请确保保留这里的id,因为如果您使用自定义TextView,TabLayout会检查此ID。

然后从代码中膨胀此布局并在TextView上设置自定义字体,并将此自定义视图添加到选项卡中。

for (int i = 0; i < tabLayout.getTabCount(); i++) {
    //noinspection ConstantConditions
 TextView tv=(TextView)LayoutInflater.from(this).inflate(R.layout.custom_tab,null)
 tv.setTextColor(customColor)
 tabLayout.getTabAt(i).setCustomView(tv);

}

tabLayout.getTabAt(i).setCustomView(tv); 它会将视图添加到堆栈中,有什么避免视图堆叠的想法吗? - waseem

0
 <com.google.android.material.tabs.TabLayout
            android:id="@+id/color"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:tabTextColor="#FFFFFF"
            app:layout_constraintStart_toStartOf="parent"
            >

            <com.google.android.material.tabs.TabItem
                android:id="@+id/showcase"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="TAB 1"
                />

            <com.google.android.material.tabs.TabItem
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="TAB 2" />

            <com.google.android.material.tabs.TabItem
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="TAB 3" />
        </com.google.android.material.tabs.TabLayout>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接