21

我试图让ViewPagerIndicator中的SampleTabsStyled演示更改当前选定选项卡的文本颜色,但没有成功。

但是,SampleTitlesStyledTheme 演示在标题/选项卡之间切换时确实会更改其文本颜色。

查看样式 xml时:

<resources>
    ...
    <style name="CustomTitlePageIndicator">
        <item name="android:background">#18FF0000</item>
        <item name="footerColor">#FFAA2222</item>
        <item name="footerLineHeight">1dp</item>
        <item name="footerIndicatorHeight">3dp</item>
        <item name="footerIndicatorStyle">underline</item>
        <item name="android:textColor">#AA000000</item>
        <item name="selectedColor">#FF000000</item>
        <item name="selectedBold">true</item>
    </style>
    ...
    <style name="CustomTabPageIndicator" parent="Widget.TabPageIndicator">
        <item name="android:background">@drawable/custom_tab_indicator</item>
        <item name="android:textAppearance">@style/CustomTabPageIndicator.Text</item>
        <item name="android:textColor">#FF555555</item>
        <item name="android:textSize">16sp</item>
        <item name="android:divider">@drawable/custom_tab_indicator_divider</item>
        <item name="android:dividerPadding">10dp</item>
        <item name="android:showDividers">middle</item>
        <item name="android:paddingLeft">8dp</item>
        <item name="android:paddingRight">8dp</item>
        <item name="android:fadingEdge">horizontal</item>
        <item name="android:fadingEdgeLength">8dp</item>
    </style>

    <style name="CustomTabPageIndicator.Text" parent="android:TextAppearance.Medium">
        <item name="android:typeface">monospace</item>
    </style>
    ...
</resources>

我看到SampleTitlesStyledTheme演示使用了CustomTitlePageIndicator样式,它定义了一个selectedColor项目。所以(也许天真地)我想添加

<item name="selectedColor">#FF000000</item>

对于SampleTabsStyled演示使用的样式CustomTabPageIndicator,但是,唉,那行不通。

这个问题似乎很明显,但我还是会问:有没有办法(使用当前样式 xml)让SampleTabsStyled演示中的选项卡的当前选定文本具有与其他选项卡不同的颜色?如果是这样,怎么做?

编辑

哦,我将它与 ActionBarSherlock 结合使用,以防万一这很重要......

4

2 回答 2

43

假设您不介意创建额外的 xml,首先尝试android:textColor在您的CustomTabPageIndicator.Text(or CustomTabPageIndicator) 中包含该属性,如下所示:

<style name="CustomTabPageIndicator.Text" parent="android:TextAppearance.Medium">
    <item name="android:textColor">@drawable/tab_text_color</item>
    ...
</style>

tab_text_color.xml放在 res/drawable 文件夹下的位置:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_selected="true"
        android:color="@color/text_tab_selected" />
    <item
        android:state_selected="false"
        android:color="@color/text_tab_unselected" />
</selector>

最后,只需在 res/values 文件夹中的颜色资源文件中定义这两种颜色@color/text_tab_selected@color/text_tab_unselected如果colors.xml它不存在,则创建一个)。例如:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="text_tab_selected">#FF000000</color>
    <color name="text_tab_unselected">#FF555555</color>
</resources>
于 2013-05-18T19:18:41.327 回答
16

试试这个

最简单的方法

<android.support.design.widget.TabLayout
                xmlns:app="http://schemas.android.com/apk/res-auto"
                android:id="@+id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:tabTextColor="@color/White"
                app:tabSelectedTextColor="@color/Blue"
                app:tabIndicatorColor="@color/Yellow"
                app:tabMode="fixed"
                app:tabGravity="fill"/>

不要忘记添加此依赖项

compile 'com.android.support:design:23.1.1'
于 2016-01-02T11:24:45.040 回答