按钮背景选择器
如果按下按钮,我尝试切换按钮的背景。我构建了一个选择器,就像这里建议的答案一样: 具有不同颜色的标准 Android 按钮< /a>
最后,我想将 GradientDrawables
放入其中,但出于调试目的,我只是设置了一种颜色,以检查它是否有效。 这是我的选择器
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/red"/>
<item
android:state_focused="true"
android:drawable="@color/white"/>
<item
android:state_pressed="true"
android:drawable="@color/white"/>
</selector>
不幸的是这不起作用。我将选择器设置为按钮背景,但只能看到它们为红色。我做错了什么(构建目标 2.1)
I try to switch the background of Buttons if they are pressed. I build a Selector like the answer suggested here: Standard Android Button with a different color
Finally I want to put GradientDrawables
inside, but for debugging purposes I just set a color, to check if it works.
Here is my Selector
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/red"/>
<item
android:state_focused="true"
android:drawable="@color/white"/>
<item
android:state_pressed="true"
android:drawable="@color/white"/>
</selector>
Unfortunatly this doesn't work. I set the Selector as Button background and only see them in red color. What Am I doing wrong (Build Target 2.1)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
把它放在最后
一项 android:drawable="@color/red"
我的意思是作为第三个选项,它会起作用。
android从一开始就检查xml条件,第一个标签没有任何条件,所以它总是选择红色,所以你把条件放在第一位,然后是默认的。
put this at the end
item android:drawable="@color/red"
i mean as the third option, it will work.
android checks the xml conditions from the start, the first tag doesn't have any condition, so it will always pick red, so you have put conditions first and then the default one.
这是我使用的代码,效果非常好。
在这里,我使用使用 Photoshop 制作的两张图像作为背景,
第一个是 button_clicked,第二个是 button
复制它并更改使用您自己的资源。
希望我能帮忙:)
here is the code I use, and it works really well.
here I use two images I made using photoshop as a background
the first is button_clicked and the second is button
copy it and change use your own resources.
hope I could help :)