3
adapter.addTab(getSupportActionBar().newTab().setText("Tab-1"),
                Tab1.class, null);
adapter.addTab(getSupportActionBar().newTab().setText("Tab-2"),
                Tab2.class, null);
adapter.addTab(getSupportActionBar().newTab().setText("Tab-3"),
                Tab3.class, null);

到目前为止,每个选项卡的 TextColor 都为白色。我希望它在未选中时为灰色,在选中时为白色。那么,如何更改onTabSelectedonTabUnselected中的文本颜色。

还是我应该使用 setCustomView 作为选项卡?这里又是 TextSize 和所有需要照顾的

<style name="my_ActionBarTabStyle" parent="@style/Widget.Sherlock.ActionBar.TabView">
    <item name="background">@drawable/tab_indicator_ab_wicfy</item>
    <item name="android:background">@drawable/tab_indicator_ab_wicfy</item>
    <item name="android:textColor">@color/black</item>
</style>

我试着用

<item name="textColor">@color/black</item>

但它给了我一个错误,即textColor不是一个有效的属性

谢谢你

4

1 回答 1

15

您不应更改代码中的文本颜色。请改用颜色状态列表资源

在资源中定义颜色选择器。在目录中定义一个 xml 文件res/color/。该文件将包含:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- use when selected -->
    <item android:state_selected="true" android:color="#fff" />

    <!-- otherwise use -->
    <item android:color="#888" />
</selector>

然后在样式中设置文本颜色:

<item name="android:textColor">@color/my_color_selector</item>

编辑:

您必须在样式中的正确位置设置文本颜色!将 textColor 设置为(android:)actionBarTabTextStyle. 主题必须包含:

<style name="MyTheme" parent="Theme.Sherlock.Light.DarkActionBar">
    ...
    <!-- define text style for tabs -->
    <item name="actionBarTabTextStyle">@style/MyTabTextStyle</item>
    <item name="android:actionBarTabTextStyle">@style/MyTabTextStyle</item>
    ...
</style>

然后在选项卡文本样式中设置文本颜色:

<style name="MyTabTextStyle" parent="Widget.Sherlock.ActionBar.TabText" >
    <item name="android:textColor">@color/my_color_selector</item>
</style>
于 2012-11-08T11:59:53.397 回答