2016-01-20 34 views
2

我有以下問題:繪圖資源引用屬性

我的應用程序有一盞燈,一個黑暗的主題,我想觸摸反饋適用於兩個V21上面和下面的一些自定義視圖(紋波觸摸反饋) 。我創建了兩個可繪製資源drawable-v21的切換按鈕,一個指示燈,一個暗:

黑暗:

<?xml version="1.0" encoding="utf-8"?> 
<ripple xmlns:android="http://schemas.android.com/apk/res/android" 
    android:color="?attr/color_control_highlight_default"> 
    <item android:id="@android:id/mask" android:drawable="@drawable/dark_button_border"/> 
    <item android:drawable="@drawable/button_toggle_states"/> 
</ripple> 

光:

<?xml version="1.0" encoding="utf-8"?> 
<ripple xmlns:android="http://schemas.android.com/apk/res/android" 
    android:color="?attr/color_control_highlight_default"> 
    <item android:id="@android:id/mask" android:drawable="@drawable/light_button_border" /> 
    <item android:drawable="@drawable/button_toggle_states" /> 
</ripple> 

可以看出這兩個資源參考另一個drawable,其定義按鈕的不同狀態的顏色:

button_toggle_states

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:drawable="?attr/color_primary_1" android:state_focused="true" /> 
    <item android:drawable="?attr/color_primary_1" android:state_checked="true" /> 
    <item android:drawable="?attr/color_primary_1" android:state_selected="true" /> 
    <item android:drawable="?attr/button_border" /> 
</selector> 

這個drawable引用屬性之間的光和黑暗的主題之間改變,這就是我的問題。當試圖使用這個drawable我得到一個運行時錯誤,button_toggle_statesres/drawable-v21找不到。當我改變drawable引用顏色和其他drawables,而不是屬性,即

<?xml version="1.0" encoding="utf-8"?> 

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:drawable="@color/lt_blue" android:state_focused="true" /> 
    <item android:drawable="@color/lt_blue" android:state_checked="true" /> 
    <item android:drawable="@color/lt_blue" android:state_selected="true" /> 
    <item android:drawable="@drawable/light_button_border" /> 
</selector> 

一切工作正常。但有了這個解決方案,我將不得不製作兩個button_toggle_state文件,一個用於黑暗,另一個用於輕量級主題,而我認爲這不應該是必需的,因爲我們可以在v21及更高版本中引用屬性。

有沒有什麼我在做錯或引用drawable中的屬性,另一個drawable只使用不支持的屬性?

回答

0

我遇到了同樣的問題,無法得到這個運行。我的解決方法是將drawable本身作爲主題中的參考,然後有兩個Drawable:一個用於光照,另一個用於黑暗。

參見: https://code.google.com/p/android/issues/detail?id=26251

How to reference colour attribute in drawable?

+0

我明白了,感謝您的鏈接。這個問題似乎是關於不能在drawables中引用屬性,但對於我來說這是不正確的,因爲我可以在波紋資源中引用attr/color_control_highlight_default,它工作正常...... – Joris

+0

它是否在21之前或之後, 21? –

+0

這是所有21後,我有完全分開的資源下面21 – Joris