按钮背景选择器

发布于 2024-12-03 09:37:27 字数 720 浏览 3 评论 0原文

如果按下按钮,我尝试切换按钮的背景。我构建了一个选择器,就像这里建议的答案一样: 具有不同颜色的标准 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

素染倾城色 2024-12-10 09:37:28

把它放在最后

一项 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.

神妖 2024-12-10 09:37:28

这是我使用的代码,效果非常好。

 <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/button_clicked"
        android:state_pressed="true" android:state_enabled="true" />

    <item android:drawable="@drawable/button" android:state_enabled="true" />
    </selector>

在这里,我使用使用 Photoshop 制作的两张图像作为背景,

第一个是 button_clicked,第二个是 button

复制它并更改使用您自己的资源。

希望我能帮忙:)

here is the code I use, and it works really well.

 <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/button_clicked"
        android:state_pressed="true" android:state_enabled="true" />

    <item android:drawable="@drawable/button" android:state_enabled="true" />
    </selector>

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 :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文