具有不同背景颜色的 Android 按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3738886/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:36:39  来源:igfitidea点击:

Android button with different background colors

androidandroid-layoutandroid-xml

提问by Daniel

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

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

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

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

<?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"** /> 

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

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

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)

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

回答by Konstantin Burov

As your error states, you have to define drawableattibute 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>

Also note that drawableattribute doesn't accept raw color values, so you have to define the colors as resources. Create colors.xmlfile at res/valuesfolder:

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

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

回答by Sreeram

In the URL you pointed to, the button_text.xml is being used to set the textColor attribute.That it is reason they had the button_text.xml in res/color folder and therefore they used @color/button_text.xml

在您指向的 URL 中,button_text.xml 用于设置 textColor 属性。这就是他们在 res/color 文件夹中有 button_text.xml 的原因,因此他们使用 @color/button_text.xml

But you are trying to use it for background attribute. The background attribute looks for something in res/drawable folder.

但是您正试图将它用于背景属性。background 属性在 res/drawable 文件夹中查找内容。

check this i got this selector custom button from the internet.I dont have the link.but i thank the poster for this.It helped me.have this in the drawable folder

检查这个我从互联网上得到了这个选择器自定义按钮。我没有链接。但我感谢海报。它帮助了我。把它放在可绘制文件夹中

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" >
        <shape>
            <gradient
                android:startColor="@color/yellow1"
                android:endColor="@color/yellow2"
                android:angle="270" />
            <stroke
                android:width="3dp"
                android:color="@color/grey05" />
            <corners
                android:radius="3dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>

    <item android:state_focused="true" >
        <shape>
            <gradient
                android:endColor="@color/orange4"
                android:startColor="@color/orange5"
                android:angle="270" />
            <stroke
                android:width="3dp"
                android:color="@color/grey05" />
            <corners
                android:radius="3dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>

    <item>        
        <shape>
            <gradient
                android:endColor="@color/white1"
                android:startColor="@color/white2"
                android:angle="270" />
            <stroke
                android:width="3dp"
                android:color="@color/grey05" />
            <corners
                android:radius="3dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>

</selector>

And i used in my main.xml layout like this

我在我的 main.xml 布局中使用了这样的

<Button android:id="@+id/button1"
            android:layout_alignParentLeft="true"
            android:layout_marginTop="150dip"
            android:layout_marginLeft="45dip"
            android:textSize="7pt"
            android:layout_height="wrap_content"
            android:layout_width="230dip"
            android:text="@string/welcomebtntitle1"
            android:background="@drawable/custombutton"/>

Hope this helps. Vik is correct.

希望这可以帮助。维克是对的。

EDIT :Here is the colors.xml

编辑:这是colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <color name="yellow1">#F9E60E</color>
   <color name="yellow2">#F9F89D</color>
   <color name="orange4">#F7BE45</color>
   <color name="orange5">#F7D896</color>
   <color name="blue2">#19FCDA</color>
   <color name="blue25">#D9F7F2</color>
   <color name="grey05">#ACA899</color>
   <color name="white1">#FFFFFF</color>
   <color name="white2">#DDDDDD</color>
</resources>

回答by Vik

You have to put the selector.xml file in the drwable folder. Then write: android:background="@drawable/selector". This takes care of the pressed and focussed states.

您必须将 selector.xml 文件放在 drwable 文件夹中。然后写: android:background="@drawable/selector"。这会处理按下和聚焦状态。

回答by ovnis

in Mono Android you can use filter like this:

在 Mono Android 中,您可以像这样使用过滤器:

your_button.Background.SetColorFilter(new Android.Graphics.PorterDuffColorFilter(Android.Graphics.Color.Red, Android.Graphics.PorterDuff.Mode.Multiply));