使用索引更改 TabLayout 中特定选项卡的 selected/unselected 文本颜色(仅样式一个选项卡)

Change the selected/unselected text colour of a specific Tab in a TabLayout with an index (only style one tab)

目前,我正尝试在 TabLayout 中设置一个 Tab 与其他样式不同的样式。我想要一个具有特定索引的 Tab 有红色文本,包括选中和未选中。因为我只需要一个 Tab 在特定索引处这样做,所以下面的通常解决方案不起作用:

app:tabSelectedTextColor="@color/red"
app:tabTextColor="@color/red"

我还尝试在返回特定 Tab 的标题时应用 Spannable,但这实际上并没有显示:

@Override
public CharSequence getPageTitle(int position) {
    switch (position) {
        case TAB_ONE:
            return getString(R.string.tab_one);
        case TAB_TWO_RED:
            Spannable spannable = new SpannableString(getString(R.string.tab_two));
            spannable.setSpan(new ForegroundColorSpan(Color.RED),
                        0, getString(R.string.tab_two).length(),
                        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            return spannable;
        }
        return null;
    }

如果有人知道如何为仅具有指定索引的 Tab 设置文本样式,我将不胜感激。

这很简单。

尝试将 customView 设置为 TabLayout 中的特定选项卡,如下所示:

TabLayout tabLayout;
tabLayout = (TabLayout) findViewById(R.id.pTabs);
tabLayout.getTabAt(1).setCustomView(R.layout.tab_two_layout);

自定义布局 (R.layout.tab_two_layout) 的 XML 看起来类似于:

<?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:orientation="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <TextView
        android:textStyle="bold"
        android:layout_marginLeft="5dp"
        android:layout_marginStart="5dp"
        android:layout_width="60dp"
        android:textColor="@color/red"
        android:layout_gravity="center_vertical"
        android:text="TEXT HERE"
        android:layout_height="wrap_content"/>

</LinearLayout>