2013-06-23 64 views
1

我想在按下時更改動作背景圖標的顏色。選擇時更改動作背景顏色(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> 

我怎樣才能做到這一點?

回答

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

這種方法在理論上應該工作,但我的應用程序的minSdkVersion 8所以它給了我這個錯誤:機器人:actionBarItemBackground需要API級別14(當前最小值是8)沒有這條線,它不工作...有什麼解決方法嗎? – yahya

+0

清潔工程解決了這個問題。工作得很好!謝謝。 – yahya

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>