按钮背景选择按钮、背景

2023-09-09 21:04:32 作者:寒笑

我试试,如果他们是pressed切换按钮的背景。我建立一个像答案一个选择这里建议:Standard Android的按键采用不同的颜色

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

最后我想提出GradientDrawables里面,但对于调试目的,我只是想设置一个颜色,看它是否工作。 这是我的选择。

finally I want to put GradientDrawables inside, but for debug purposes I only want to set a color, to see it is working. 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>

不幸的是,这并不正常工作。我设置的选择从我的按钮背景,只看到他们在红色。我究竟做错了(构建目标2.1)

unfortunatly this doesn't work. I set the Selector as Background from my Button, and only see them in red color. What Am I doing wrong (Build Target 2.1)

推荐答案

把这个在最后

项机器人:可绘制=@色/红色

item android:drawable="@color/red"

我的意思是作为第三个选项,它会奏效。 安卓检查XML条件从一开始,第一个标签没有任何条件,所以它始终会选择红色,那么你已经把情况先升后默认的。

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.