簡體   English   中英

Android,如何在狀態更改時更改操作欄徽標按鈕?

[英]Android, How to change the actionbars logo button on state change?

因此,我想知道如何通過徽標按鈕(每個狀態(按下,釋放等))將其實現到帶有徽標按鈕的操作欄。

我知道如何對普通按鈕執行此操作,但是我不確定用actionbars徽標按鈕是否可行?

如果不是,那么如何實現支持此功能的動作欄呢? 外部庫?

謝謝。

res/drawable創建一個drawable xml文件,例如res/drawable/button_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/button_pressed"
      android:state_pressed="true" />
    <item android:drawable="@drawable/button_normal" />
</selector>

其中button_pressedbutton_normal只是可繪制目錄之一中的常規png文件(例如, drawables-hdpi

然后,在menu.xml或代碼中,您可以引用@drawable/button_selectorR.drawable.button_selector ,按下按鈕后圖像將自動更改。

如果需要更改ActionBar按鈕的背景,則需要覆蓋res/values/style.xml的主題。

<style name="YourTheme.Sherlock" parent="@style/Theme.Sherlock">
    <item name="actionBarItemBackground">@drawable/actionbar_item_background_selector</item>
    <item name="android:actionBarItemBackground">@drawable/actionbar_item_background_selector</item>
</style>

這兩行都很重要:一個用於SherlockActionBar,另一個用於標准ActionBar。

然后將主題設置為清單中的活動:

   <activity
        android:name=".yourActivity"
        android:theme="@style/YourTheme.Sherlock" >
    </activity>

這是放置在res/drawable actionbar_item_background_selector.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">

<!-- 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/scene_overlay_bar_pressed_center"     />
    <item android:state_focused="true"  android:state_enabled="false"                                  android:drawable="@drawable/abs__list_selector_disabled_holo_light" />
    <item android:state_focused="true"                                android:state_pressed="true" android:drawable="@drawable/scene_overlay_bar_pressed_center" />
    <item android:state_focused="false"                               android:state_pressed="true" android:drawable="@drawable/scene_overlay_bar_pressed_center" />
    <item android:state_focused="true"                                                             android:drawable="@drawable/abs__list_focused_holo" />
    <item                                                                                          android:drawable="@android:color/transparent" />

</selector>

用您選擇的drawable替換背景,其中android:state_pressed="true"

僅背景就可以做很多事情,但這就是ActionBar工作方式。

您可以使用選擇器選擇其他圖像。 UI禁用的Android ImageButton可能會有所幫助

我認為您正在尋找:

final ActionBar bar = getActionBar();
bar.setDisplayHomeAsUpEnabled(true);

這將自動使您設置的徽標看起來像在單擊時被按下。 無需使用其他圖像即可顯示狀態變化。 那是一個非常簡單的方法。 我認為,盡管如果您使用的是ActionBarSherlock,將無法正常工作。 我相信絕對是4.0。 ActionBar上查看文檔。

暫無
暫無

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

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