1

目前,我正在尝试以一种与其他风格Tab不同TabLayout的方式来设计风格。我希望Tab具有特定索引的一个具有选定和未选定的红色文本。由于我只需要Tab特定索引处的一个,因此以下通常的解决方案不起作用:

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

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

@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仅具有指定索引的文本设置样式,我将不胜感激。

4

1 回答 1

3

这很简单。

尝试将 a 设置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>
于 2017-01-17T16:38:34.437 回答