2012-12-03 30 views

回答

3

res/drawableres/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在你繪製資源目錄之一(例如/ drawables-hdpi

只是普通的PNG文件創建一個可繪製的xml文件

然後在你的menu.xml或你的代碼中,你可以參考@drawable/button_selectorR.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如何工作。

+0

你好,這是否也取代了默認的藍色突出顯示按下時? – rennoDeniro

+0

有更多的工作要做,以取代背景。看到編輯答案 – znat

+0

我看到你參考Shelock欄,這意味着我需要實現這個第三方庫。所以即時猜測沒有lib和只是標準的操作欄沒有辦法做到這一點? – rennoDeniro

0

我認爲你正在尋找這樣的:

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

這將自動已設置看起來像它的點擊時按下它的標誌。不需要有不同的圖像來顯示狀態變化。這是一個非常簡單的方法。雖然如果你使用ActionBarSherlock,我認爲這是行不通的。我相信嚴格4.0。查看ActionBar上的文檔。