gpt4 book ai didi

android - 结合 StateListDrawable 和 LevelListDrawable

转载 作者:太空宇宙 更新时间:2023-11-03 12:12:18 24 4
gpt4 key购买 nike

有没有办法结合 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>

关于android - 结合 StateListDrawable 和 LevelListDrawable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6344521/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com