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

Sad*_*ife 15 layout android android-tablayout

我有这个代码用于更改标签布局文本的颜色,但它根本不起作用!

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" />
Run Code Online (Sandbox Code Playgroud)

AGM*_*zim 47

试试吧 -

<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"/>
Run Code Online (Sandbox Code Playgroud)


Far*_*ABZ 9

您可以自定义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"
/>
Run Code Online (Sandbox Code Playgroud)

请确保将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);

}
Run Code Online (Sandbox Code Playgroud)


May*_*arg 9

使用此代码,它将有助于所有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>
Run Code Online (Sandbox Code Playgroud)

当标签布局改变位置时,这将有助于我.