具有不同背景颜色的 Android 按钮 [英] Android button with different background colors

查看:36
本文介绍了具有不同背景颜色的 Android 按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 selector-xml-file 更改按钮的背景颜色.我的方法基本上是本页底部示例中的方法:http://developer.android.com/guide/topics/resources/color-list-resource.html

i want to change the background-color of a button using a selector-xml-file. My approach is basically the one from the example at the bottom this page: http://developer.android.com/guide/topics/resources/color-list-resource.html

我有一个 res/color/button_text.xml,它看起来像这样:

i have a res/color/button_text.xml which looks like this:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
          android:color="#ffff0000"/> <!-- pressed -->
    <item android:state_focused="true"
          android:color="#ff0000ff"/> <!-- focused -->
    <item android:color="#ff000000"/> <!-- default -->
</selector>

我的布局包含以下代码:

and my layout contains the following code:

<Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/button_text"
    **android:background="@color/button_text"** /> 

(** 只是为了向您展示我使用 android:background 而不是 android:textcolor)

(** is only there to show you that i use android:background instead of android:textcolor)

此代码崩溃.它说二进制 XML 文件第 4 行标签需要‘drawable’属性或定义 drawable 的子标签.但是如果我按照上面链接中的描述使用 android:textColor 尝试它,它工作正常.所以它必须是背景问题.我如果没有必要,不想创建 9patch-png(基本上我只需要一个可点击"矩形,所以我使用一个带有彩色背景的按钮)

this code crashes. it says "Binary XML file line #4 tag requires 'drawable' attribute or child tag defining drawable. But if I try it with android:textColor as described in the above link it works fine. So it has to be the background issue. I don't want to create a 9patch-png if it's not necessary (basically i just need a "clickable" rectangle so i use a button with a colored background)

推荐答案

正如您的错误所述,您必须为项目定义 drawable 属性(出于某种原因,当涉及到背景时需要它定义),所以:

As your error states, you have to define drawable attibute for the items (for some reason it is required when it comes to background definitions), so:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@color/red"/> <!-- pressed -->
    <item android:state_focused="true" android:drawable="@color/blue"/> <!-- focused -->
    <item android:drawable="@color/black"/> <!-- default -->
</selector>

另请注意,drawable 属性不接受原始颜色值,因此您必须将颜色定义为资源.在 res/values 文件夹中创建 colors.xml 文件:

Also note that drawable attribute doesn't accept raw color values, so you have to define the colors as resources. Create colors.xml file at res/values folder:

<?xml version="1.0" encoding="utf-8"?>
<resources>
     <color name="black">#000</color>
     <color name="blue">#00f</color>
     <color name="red">#f00</color>
</resources>

这篇关于具有不同背景颜色的 Android 按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆