2012-09-12 47 views
7

我想添加到我現有的狀態列表drawable,禁用狀態,它只是不工作。Statelist可繪製android:state_enabled不能在xml工作

最初,我有這個代碼:

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

,併爲選定的,而不是選擇完美。

現在我想添加了android:state_enabled =「假」是這樣的:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<item android:drawable="@drawable/store_item_background_selected" android:state_selected="true"/> 
<item android:drawable="@drawable/store_item_background" android:state_enabled="true"/> 
<item android:drawable="@drawable/store_item_background_disabled"/> 
</selector> 

和它永遠不會切換到殘疾人士的圖像。

有什麼想法?

編輯

我加入setEnabled(false)到我設置這個statelist drwable,現在我看到了禁用圖像視圖的構造,但一旦我視圖設置爲啓用,也不會再次切換爲禁用。

回答

7

試試這個

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 

<item android:drawable="@drawable/store_item_background_selected" android:state_enabled="true" android:state_selected="true"/> 
<item android:drawable="@drawable/store_item_background" android:state_enabled="true"/> 
<item android:drawable="@drawable/store_item_background_disabled" android:state_enabled="false"/> 

</selector> 
+0

沒有有所作爲。請參閱編輯 – piojo

+0

對不起,在我的代碼中將其設置爲禁用的錯誤。 – piojo

+0

不好回答。沒有解釋NOR工作。 – Vucko

15

雖然這是一個非常古老的問題。 On應該相應地寫入選擇器。

  1. 禁用狀態,第一
  2. 按下狀態第二
  3. 正常狀態最後

+0

切換訂單立即解決了我的問題。你在哪裏找到這些信息? –

+0

一些在谷歌當我被困在這個相同的問題小時 –

+0

我有這個問題很長一段時間使用這種方法: StateListDrawable res = new StateListDrawable(); res.addState(new int [] { - android.R.attr.state_enabled},new ColorDrawable(color)); (new int [] {android.R.attr.state_enabled,android.R.attr.state_pressed},new ColorDrawable(color)); res.addState(new int [] {},new ColorDrawable(color)); 而這立即工作。謝謝。希望它被記錄在某處... – ClayHerendeen