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

How to Change Text Color of tab Layout?

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

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" />

您可以自定义 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);

}

尝试一下 -

<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"/>

使用此代码将有助于所有 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>

这会有所帮助 u.it 在选项卡布局更改位置时帮助我。

 <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>