当我选择页面时,如何设置PagerTabStrip的文本颜色不同

时间:2014-01-10 03:01:22

标签: android android-viewpager

有一种名为setTextColor(),的方法,但很难显示出不同的颜色,这取决于我选择页面的状态。我想手动设置颜色。

3 个答案:

答案 0 :(得分:1)

你的textview propeties中的

android:textColor="@color/text_selector"
res / color text_selector.xml

中的

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

答案 1 :(得分:1)

你唯一要做的就是写下你的颜色状态列表:

pager_tab_strip_text_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:color="@color/background_black_3" android:state_window_focused="false" />
    <item android:color="@color/highlight_gold" android:state_window_focused="true" />

</selector>

将颜色状态列表放在样式中:

<style name="PagerTabStrip">
        <item name="android:textColor">@color/pager_tab_strip_text_selector</item>
    </style>

您必须做的唯一事情是,不要将其设置为样式,而是将其设置为textAppearance,如下所示:

<android.support.v4.view.ViewPager
        android:id="@+id/fragment_light_viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v4.view.PagerTitleStrip
            android:id="@+id/fragment_light_pagertabstrip"
            android:textAppearance="@style/PagerTabStrip.Light"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/transparent" />

    </android.support.v4.view.ViewPager>

答案 2 :(得分:0)

这个选择器可以实现:

<?xml version="1.0" encoding="utf-8"?>

<!-- For the font selection states to work, a drawable needs to be specified as well 
    (even though it is not used) not sure why.-->

<selector xmlns:android="http://schemas.android.com/apk/res/android" >

    <!-- Tab is active  -->
    <item   android:state_checked="true" 
            android:color="@color/view_pager_active_tab_color"
            android:drawable="@drawable/my_dummy_drawable" /> 

    <!-- Default -->
    <item   android:color="@color/view_pager_inactive_tab_color" 
            android:drawable="@drawable/my_dummy_drawable" /> 

</selector>