选中时更改动作背景颜色(ActionBarSherlock)

时间:2013-06-23 16:54:23

标签: android background actionbarsherlock

我想在按下动画背景图标时更改动画背景图标的颜色。

我尝试了以下但没有结果:

styles.xml

<style name="Theme.Styled" parent="Theme.Sherlock.Light.DarkActionBar">
    <item name="actionBarStyle">@style/Widget.Styled.ActionBar</item>
    <!-- <item name="android:actionBarStyle">@style/Widget.Styled.ActionBar</item> -->
</style>

<style name="Widget.Styled.ActionBar" parent="Widget.Sherlock.Light.ActionBar.Solid.Inverse">
    <item name="actionBarItemBackground">@drawable/action_item_background</item>
    <item name="selectableItemBackground">@drawable/action_item_background</item>
</style> 

action_item_background.xml

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

    <solid android:color="#FF0000"/>

</shape>

我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:10)

我找到的解决方案是:

<强> action_item_background.xml

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

    <solid android:color="#FF0000"/>

</shape>

<强> action_item_background_selector.xml

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

    <item 
        android:state_pressed="true" 
        android:drawable="@drawable/action_item_background" />
    <item 
        android:drawable="@android:color/transparent" />

</selector>

<强> styles.xml

 <style name="Theme.Styled" parent="Theme.Sherlock.Light.DarkActionBar">
            <item name="actionBarStyle">@style/Widget.Styled.ActionBar</item>
            <item name="android:actionBarStyle">@style/Widget.Styled.ActionBar</item>
            <item name="actionBarItemBackground">@drawable/action_item_background_selector</item>
            <item name="android:actionBarItemBackground">@drawable/action_item_background_selector</item>
        </style>

        <style name="Widget.Styled.ActionBar" parent="Widget.Sherlock.Light.ActionBar.Solid.Inverse">
        //...
    </style>

答案 1 :(得分:0)

您必须使用选择器slector_action_item_background:

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

    <item android:state_pressed="true" android:drawable="@drawable/action_item_background" />
    <item android:drawable="@android:color/transparent" />
</selector>

然后用你的风格替换它

<style name="Widget.Styled.ActionBar" parent="Widget.Sherlock.Light.ActionBar.Solid.Inverse">
    <item name="actionBarItemBackground">@drawable/selector_action_item_background</item>
</style>