有没有办法结合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
更改drawable的级别(_btn是一个按钮顺便说一句。)
为什么它不起作用?
提前致谢。
答案 0 :(得分:8)
找到自己的解决方案:&lt; selector&gt;元素必须包含在&lt; item&gt;中元素:
<?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>
答案 1 :(得分:0)
就我而言,问题是我没有将按钮设置为使用级别列表中的背景。例如,如果您在res / drawable / button_levels.xml中定义了level-list,则需要将按钮背景设置为“@ drawable / button_levels”。