使用ViewPagerIndicator为选定选项卡的文本指定不同的颜色

Bar*_*ers 21 android actionbarsherlock viewpagerindicator

我试图让SampleTabsStyled演示ViewPagerIndicator改变当前选择的标签没有成功文本的颜色.

但是,在标题/选项卡之间切换时,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>
Run Code Online (Sandbox Code Playgroud)

我看到SampleTitlesStyledTheme演示使用了CustomTitlePageIndicator定义selectedColor项目的样式.所以(也许天真)我想补充一下

<item name="selectedColor">#FF000000</item>
Run Code Online (Sandbox Code Playgroud)

对于SampleTabsStyled演示所使用的风格CustomTabPageIndicator,但是,唉,这不起作用.

问题似乎很明显,但无论如何我会问:是否有一种方法(使用现有的样式xml)让SampleTabsStyled演示中选项卡的当前选定文本具有与其他选项卡不同的颜色?如果是这样,怎么样?

编辑

哦,我正在将它与ActionBarSherlock结合使用,以防重要......

Neo*_*eoh 43

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

<style name="CustomTabPageIndicator.Text" parent="android:TextAppearance.Medium">
    <item name="android:textColor">@drawable/tab_text_color</item>
    ...
</style>
Run Code Online (Sandbox Code Playgroud)

其中,tab_text_color.xml下水库/可绘制文件夹放:

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

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

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="text_tab_selected">#FF000000</color>
    <color name="text_tab_unselected">#FF555555</color>
</resources>
Run Code Online (Sandbox Code Playgroud)


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

别忘了添加此依赖项

compile 'com.android.support:design:23.1.1'
Run Code Online (Sandbox Code Playgroud)