如何在Android 3.0及以上版本中更改ActionBar的触摸效果颜色?

23

我想要改变当你触摸ActionBar项目时的鼠标悬停效果的颜色。 在我的Galaxy Nexus上,版本为4.0.2时,它是一种绿松石色调,但我想要换成其他颜色。

需要明确的是,我在这里谈论的是ActionBar项目,而不是导航选项卡。

我已经在兼容库下成功实现了这个功能,但对于Android 3.0及更高版本,即“真正”的ActionBar,我就不知道该如何做了。

是否有人知道如何实现这个功能呢?

1个回答

46

原生的操作栏使用主题属性selectableItemBackground来绘制操作项的背景。这应该是一个状态列表可绘制对象。

以下是在Theme.Holo中的声明:

<style name="Theme.Holo">
    <!-- bunch of things -->
    <item name="android:selectableItemBackground">@android:drawable/item_background_holo_dark</item>
    <!-- bunch of things -->
</style>

这是它的可绘制XML:

<selector xmlns:android="http://schemas.android.com/apk/res/android"
          android:exitFadeDuration="@android:integer/config_mediumAnimTime">

    <!-- Even though these two point to the same resource, have two states so the drawable will invalidate itself when coming out of pressed state. -->
    <item android:state_focused="true"  android:state_enabled="false" android:state_pressed="true" android:drawable="@drawable/list_selector_disabled_holo_dark" />
    <item android:state_focused="true"  android:state_enabled="false"                              android:drawable="@drawable/list_selector_disabled_holo_dark" />
    <item android:state_focused="true"                                android:state_pressed="true" android:drawable="@drawable/list_selector_background_transition_holo_dark" />
    <item android:state_focused="false"                               android:state_pressed="true" android:drawable="@drawable/list_selector_background_transition_holo_dark" />
    <item android:state_focused="true"                                                             android:drawable="@drawable/list_focused_holo" />
    <item                                                                                          android:drawable="@color/transparent" />
</selector>

感谢您的快速回复。但是,如果我尝试在样式中使用类似于以下内容的内容覆盖此属性:<style name="AppTheme" parent="android:style/Theme.Holo.Light">
<item name="selectableItemBackground">drawable/item_background_holo_dark</item> </style> 我会收到错误消息:找不到与给定名称“attr selectableItemBackground”匹配的资源。我应该在哪里或如何覆盖此属性?
- Micky
抱歉,你需要使用name="android:selectableItemBackground"。它没有在Android主题的平台版本中指定,因为它的默认包已经是'android'。我更新了答案。 - Jake Wharton

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接