Is there a way to combine the effects of a StateListDrawable and a LevelListDrawable?
I wrote this resource file but when used nothing appear:
<?xml version="1.0" encoding="utf-8"?>
<level-list xmlns:android="http://schemas.android.com/apk/res/android">
<selector
android:constantSize="true"
android:variablePadding="false"
android:maxLevel="0"
>
<item android:state_pressed="true">
<layer-list>
<item android:drawable="@drawable/btu_color_standard" />
<item android:drawable="@drawable/btu_shade_pressed" />
</layer-list>
</item>
<item android:state_focused="true">
<layer-list>
<item android:drawable="@drawable/btu_color_standard" />
<item android:drawable="@drawable/btu_shade_selected" />
</layer-list>
</item>
<item>
<layer-list>
<item android:drawable="@drawable/btu_color_standard" />
<item android:drawable="@drawable/btu_shade_normal" />
</layer-list>
</item>
</selector>
<selector
android:constantSize="true"
android:variablePadding="false"
android:maxLevel="1"
>
<item android:state_pressed="true">
<layer-list>
<item android:drawable="@drawable/btu_color_standard" />
<item android:drawable="@drawable/btu_lighted" />
<item android:drawable="@drawable/btu_shade_pressed" />
</layer-list>
</item>
<item android:state_focused="true">
<layer-list>
<item android:drawable="@drawable/btu_color_standard" />
<item android:drawable="@drawable/btu_lighted" />
<item android:drawable="@drawable/btu_shade_selected" />
</layer-list>
</item>
<item>
<layer-list>
开发者_StackOverflow社区 <item android:drawable="@drawable/btu_color_standard" />
<item android:drawable="@drawable/btu_lighted" />
<item android:drawable="@drawable/btu_shade_normal" />
</layer-list>
</item>
</selector>
</level-list>
Then I call
_btn.getBackground().setLevel(level); //level is either 0 or 1
to change the level of the drawable (_btn is a button btw).
Why it doesn't work?
Thanks in advance.
Found myself the solution: <selector> elements must be enclosed in <item> elements:
<?xml version="1.0" encoding="utf-8"?>
<level-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:maxLevel="0"
>
<selector
android:constantSize="true"
android:variablePadding="false"
>
<item android:state_pressed="true">
<layer-list>
<item android:drawable="@drawable/btu_color_standard" />
<item android:drawable="@drawable/btu_shade_pressed" />
</layer-list>
</item>
<item android:state_focused="true">
<layer-list>
<item android:drawable="@drawable/btu_color_standard" />
<item android:drawable="@drawable/btu_shade_selected" />
</layer-list>
</item>
<item>
<layer-list>
<item android:drawable="@drawable/btu_color_standard" />
<item android:drawable="@drawable/btu_shade_normal" />
</layer-list>
</item>
</selector>
</item>
<item
android:maxLevel="1"
>
<selector
android:constantSize="true"
android:variablePadding="false"
>
<item android:state_pressed="true">
<layer-list>
<item android:drawable="@drawable/btu_color_standard" />
<item android:drawable="@drawable/btu_lighted" />
<item android:drawable="@drawable/btu_shade_pressed" />
</layer-list>
</item>
<item android:state_focused="true">
<layer-list>
<item android:drawable="@drawable/btu_color_standard" />
<item android:drawable="@drawable/btu_lighted" />
<item android:drawable="@drawable/btu_shade_selected" />
</layer-list>
</item>
<item>
<layer-list>
<item android:drawable="@drawable/btu_color_standard" />
<item android:drawable="@drawable/btu_lighted" />
<item android:drawable="@drawable/btu_shade_normal" />
</layer-list>
</item>
</selector>
</item>
</level-list>
In my case, the problem was I did not set the button to use the background in the level-list. For example, if you have the level-list defined in res/drawable/button_levels.xml, then you need to set the button background as "@drawable/button_levels".
精彩评论