有没有办法结合 StateListDrawable 和 LevelListDrawable 的效果?
我写了这个资源文件,但是在使用时什么也没有出现:
<?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>
<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>
然后我打电话
_btn.getBackground().setLevel(level); //level is either 0 or 1
更改可绘制对象的级别(顺便说一句,_btn 是一个按钮)。
为什么它不起作用?
提前致谢。
自己找到解决方案:
元素必须包含在
-
元素中:
<?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>
我是一名优秀的程序员,十分优秀!