2011-12-01 60 views
9

有沒有一種方法可以從已知的selector中繼承Android?選擇器繼承

我想擴展一個EditText並添加了一個自定義狀態,到目前爲止我理解使用onCreateDrawableState()方法。當選擇器進場時,有一種簡單的方法可以使用默認選擇器,只需添加我的選擇器而不是再次定義它們?

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

<item android:state_enabled="false" android:drawable="@drawable/login_textfield_bg_error" /> 
<item android:state_window_focused="false" android:drawable="@drawable/login_textfield_bg_error"> 
<item android:state_pressed="true" android:drawable="@drawable/login_textfield_bg_error" /> 
<item android:state_selected="true" android:drawable="@drawable/login_textfield_bg_error" /> 
<item app:errorBackground="@drawable/login_textfield_bg_error" /> 
</selector> 

回答

4

我可能會誤解,但也許你可以明白地委託給他們?

所以你的情況,你有一個自定義的狀態,因此,如果您的自定義狀態適用,其中僅定義的情況下你不能做到這一點:

<selector xmlns:android="..." xmlns:app="..."> 
    <item app:custom_state="true" android:drawable="@drawable/the_one_care_about"/> 
    <item android:drawable="@android:drawable/editbox_background"/> 
</selector> 

所以這基本上指出,各國在我的定製狀態是真實的,顯示我的自定義背景......但是對於所有其他狀態,其行爲與此選擇器相同。這個選擇器恰好有其他狀態的指令,所以也要遵循它們。所以沒有重新定義,並且由於狀態是按照從上到下的順序來評估的,所以在技術上不必重新定義任何東西,你只是聲明你只想定義一個狀態子集並委託給另一個可繪製對象(這發生在作爲另一個選擇器)用於所有其他內容。這有幫助嗎?