所以我想知道如何實現一個標誌按鈕,每個狀態不同的圖像(按下,釋放等)的動作欄。Android,如何更改狀態更改上的操作條標誌按鈕?
我知道如何做到這一點的普通按鈕,但我不知道是否有可能與操作條標誌按鈕?
如果不是,怎麼能實現一個支持這個動作欄?外部庫?
謝謝。
所以我想知道如何實現一個標誌按鈕,每個狀態不同的圖像(按下,釋放等)的動作欄。Android,如何更改狀態更改上的操作條標誌按鈕?
我知道如何做到這一點的普通按鈕,但我不知道是否有可能與操作條標誌按鈕?
如果不是,怎麼能實現一個支持這個動作欄?外部庫?
謝謝。
在res/drawable
如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_pressed
和button_normal
在你繪製資源目錄之一(例如/ drawables-hdpi
)
然後在你的menu.xml或你的代碼中,你可以參考@drawable/button_selector
或R.drawable.button_selector
,當按鈕被預先設定時圖像會自動改變ssed。
如果您需要更改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和一個標準動作條
然後設置主題,在清單中的活動:
<activity
android:name=".yourActivity"
android:theme="@style/YourTheme.Sherlock" >
</activity>
這裏是actionbar_item_background_selector.xml
放置in res/drawable
<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>
replace背景與你的選擇可繪製android:state_pressed="true"
這只是一個背景做很多事情,但這是ActionBar
如何工作。
您可以使用選擇器來選取不同的圖像。 Android ImageButton with disabled UI feel 可能有助於
我認爲你正在尋找這樣的:
final ActionBar bar = getActionBar();
bar.setDisplayHomeAsUpEnabled(true);
這將自動已設置看起來像它的點擊時按下它的標誌。不需要有不同的圖像來顯示狀態變化。這是一個非常簡單的方法。雖然如果你使用ActionBarSherlock,我認爲這是行不通的。我相信嚴格4.0。查看ActionBar上的文檔。
你好,這是否也取代了默認的藍色突出顯示按下時? – rennoDeniro
有更多的工作要做,以取代背景。看到編輯答案 – znat
我看到你參考Shelock欄,這意味着我需要實現這個第三方庫。所以即時猜測沒有lib和只是標準的操作欄沒有辦法做到這一點? – rennoDeniro