长时间的stackoverflow阅读器,第一次提出问题。我遇到的问题似乎很愚蠢,但我找不到任何解释它的信息。
我是 Android 编程新手,正在开发一个具有自定义按钮背景的项目。我正在为不同的按钮使用状态列表可绘制对象,如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<bitmap android:src="@drawable/button_pressed_background" android:gravity="center"/>
android:state_pressed="true"
</item>
<item>
<bitmap android:src="@drawable/button_focused_background" android:gravity="center"/>
android:state_focused="true"
</item>
<item>
<bitmap android:src="@drawable/button_standard_background" android:gravity="center"/>
android:state_pressed="false"
android:state_focused="false"
</item>
</selector>
在声明按钮的 xml 中,我只是添加了android:background="@drawable/button_drawable"
button_drawable.xml 是状态列表可绘制的行。
看起来很简单,但在所有情况下,<item>
无论其状态如何,按钮都会显示状态列表可绘制的第一部分中列出的背景。实际上,在上面的示例中,我可以将第一<item>
部分更改为包含android:state_pressed="false"
,并且button_pressed_background
仍然显示!就好像所有按钮状态同时为真和假。
我正在使用模拟器进行开发,但是在使用 AVD 模拟器和在 Oracle VirtualBox 上运行的 Android x86 时都可以看到这种行为。知道为什么会发生这种情况吗?