【问题标题】:How to change ActionBarSherlock Tab Text color?如何更改 ActionBarSherlock 选项卡文本颜色?
【发布时间】:2012-11-08 11:49:28
【问题描述】:
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 不是有效属性

谢谢

【问题讨论】:

    标签: android actionbarsherlock


    【解决方案1】:

    您不应更改代码中的文本颜色。请改用color state list resource

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

    <?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>
    

    编辑:

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

    <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>
    

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

    <style name="MyTabTextStyle" parent="Widget.Sherlock.ActionBar.TabText" >
        <item name="android:textColor">@color/my_color_selector</item>
    </style>
    

    【讨论】:

    • 喜欢问题中的 1 吗?
    • 您必须将其添加到res/color 并将其引用为@color/tab_text_indicator。当需要颜色时,您不能使用可绘制对象。
    • 但想法是改变颜色。所以我必须使用选择器对吗?这将是一个可绘制的。如果我使用的颜色会出现任何状态。
    • 除了可绘制的选择器,Android 还有颜色选择器。 textColor 项目需要颜色或颜色选择器。阅读文档中的 color selectors
    • 我只使用了 @color/black 而不是颜色选择器。如问题中所编辑,但选项卡文本颜色仍为 white
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-29
    • 1970-01-01
    • 1970-01-01
    • 2022-01-20
    • 1970-01-01
    相关资源
    最近更新 更多