2012-01-26 18 views
23

我嘗試在觸摸ActionBar項目時更改翻轉效果的顏色。 在我的Galaxy Nexus 4.0.2上,這是一種綠松石色調,我想要以不同的顏色。如何在Android 3.0及更高版本中更改ActionBar的觸摸效果顏色

要說清楚,我在這裏談論的ActionBar項目,而不是導航選項卡。

我在兼容性庫下工作,但對於Android 3.0及更高版本,即「真正的」ActionBar,我無法弄清楚如何做到這一點。

有誰知道是否以及如何實現?

回答

45

本機操作欄使用主題屬性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> 
+0

感謝您的快速反應。但是,如果我嘗試用類似於 我收到一個錯誤:找不到與給定名稱匹配的資源:attr 'selectableItemBackground'。如何或在哪裏可以覆蓋此屬性? – Alf

+0

對不起,您必須使用'name =「android:selectableItemBackground」'。它沒有在主題的Android平臺版本中指定,因爲它的默認包已經是'android'了。我更新了答案。 –

+0

完美的作品,非常感謝你! – Alf

相關問題