簡體   English   中英

按下時更改操作欄按鈕背景顏色

[英]Change actionbar button background color when pressed

如果我按下操作欄中的按鈕,那么它的背景顏色不是我想要的。 我的項目的背景顏色不響應我的點擊事件。 我如何更改它並在按下時更改背景顏色?

您需要聲明android:actionBarItemBackground屬性,它是一個:

操作欄項目的自定義項目狀態列表可繪制背景。

然后,在您的樣式中執行以下操作:

<style name="CustomStyle" parent="@style/Theme.Holo.Light" >
    <item name="android:actionBarItemBackground">@drawable/ab_item_background</item>
    <item name="actionBarItemBackground">@drawable/ab_item_background</item>
</style>  

因此,將您自己的 drawable 與selector和每個狀態(按下、聚焦、禁用等)放在一起,以獲得預期的背景。 例如,上面聲明的可繪制ab_item_background.xml可能是這樣的:

<selector xmlns:android="http://schemas.android.com/apk/res/android" 
    android:exitFadeDuration="@android:integer/config_mediumAnimTime">
    <!-- focused/pressed: color=red -->
    <item 
        android:state_focused="true"
        android:state_pressed="true"
        android:drawable="@color/red" />
    <!-- pressed: color=red -->
    <item 
        android:state_pressed="true"
        android:drawable="@color/red" />
    <!-- normal: color=transparent -->
    <item 
        android:drawable="@android:color/transparent" />
</selector>

設置操作欄樣式 中,您可以找到所有可能的自定義和所有屬性。

ActionBar actionBar = getActionBar();
actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#0a0a0a")));

這可能有幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM