Android 选项卡文本颜色选择器忽略 state_pressed

2023-12-30

我创建了自己的 Android 主题来更改操作栏选项卡的外观。问题是 textcolor 选择器似乎忽略了 state_pressed 属性,因此选项卡文本的颜色始终相同,即使按下该选项卡也是如此。其他状态没有问题,例如 state_selected 被正确识别,并且选定的选项卡具有文本颜色,与未选定的选项卡文本颜色不同。

更重要的是,我还为选项卡背景创建了选择器,它与 state_pressed 一起工作得很好(如果按下选项卡,它的背景颜色就会改变)。

我的代码有一些片段:

样式.xml:

<style name="Theme.MyTheme" parent="android:Theme.Holo.Light.DarkActionBar">
    <item name="android:actionBarTabStyle">@style/Theme.MyTheme.TabStyle</item>
    <item name="android:actionBarTabTextStyle">@style/Theme.MyTheme.TabTextStyle</item>
</style>

...

<style name="Theme.MyTheme.TabStyle"
       parent="@android:style/Widget.Holo.Light.ActionBar.TabView">
    <item name="android:background">@drawable/background_selector</item>
</style>

<style name="Theme.MyTheme.TabTextStyle"
       parent="@android:style/Widget.Holo.Light.ActionBar.TabText">
    <item name="android:textColor">@color/textcolor_selector</item>
</style>

背景选择器.xml:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="false" android:state_pressed="false">
        <shape>
            <solid android:color="#00ff00"/>
        </shape>
    </item>

    <item android:state_selected="false" android:state_pressed="true">
        <shape>
            <solid android:color="#0000ff"/>
        </shape>
    </item>

    <item android:state_selected="true" android:state_pressed="false">
        <shape>
            <solid android:color="#ff0000"/>
        </shape>
    </item>

    <item android:state_selected="true" android:state_pressed="true">
        <shape>
            <solid android:color="#ffff00"/>
        </shape>
    </item>
</selector>

文本颜色选择器.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true" android:state_pressed="false"
          android:color="#ff0000"/>
    <item android:state_selected="true" android:state_pressed="true"
          android:color="#0000ff"/>
    <item android:state_selected="false" android:state_pressed="false"
          android:color="#ffff00"/>
    <item android:state_selected="false" android:state_pressed="true"
          android:color="#00ff00"/>
</selector>

我已经尝试了一切但没有成功 - state_pressed 属性似乎被忽略,但仅在 textcolor_selector 中。请帮助我理解并解决这个问题。


回顾自定义文本颜色 https://developer.android.com/training/basics/actionbar/styling.html#CustomTextActionBar 样式文档的部分 - 它提到Note: The custom style applied to titleTextStyle should use TextAppearance.Holo.Widget.ActionBar.Title as the parent style.- 也许那里的改变可能有助于解决问题。

另一个值得一看的地方是here https://developer.android.com/guide/topics/ui/actionbar.html#Style在下面Example theme部分 - 它显示了一个示例android:actionBarTabTextStyle这可能会帮助您解决问题。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android 选项卡文本颜色选择器忽略 state_pressed 的相关文章

随机推荐