不同背景颜色的Android按钮

不同背景颜色的Android按钮,android,android-layout,android-xml,Android,Android Layout,Android Xml,我想使用选择器xml文件更改按钮的背景色。我的方法基本上是本页底部示例中的方法: 我有一个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"

我想使用选择器xml文件更改按钮的背景色。我的方法基本上是本页底部示例中的方法:

我有一个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>
your_button.Background.SetColorFilter(new Android.Graphics.PorterDuffColorFilter(Android.Graphics.Color.Red, Android.Graphics.PorterDuff.Mode.Multiply));

“我的布局”包含以下代码:

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


此代码崩溃。它说“Binary XML file line#4标记需要'drawable'属性或定义drawable的子标记。但是,如果我使用上面链接中描述的android:textColor进行尝试,它会很好地工作。因此它必须是背景问题。如果不需要,我不想创建9patch png(基本上我只需要一个“clickable”)“矩形,因此我使用带有彩色背景的按钮)

作为错误状态,您必须为项目定义可绘制的attibute(由于某些原因,当涉及背景定义时,它是必需的),因此:


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


#000
#00f
#f00

您必须将selector.xml文件放在drwable文件夹中。 然后写:
android:background=“@drawable/selector”

这将处理按下和聚焦状态。

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

但您正在尝试将其用作背景属性。background属性在res/drawable文件夹中查找内容

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

<?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>

在Mono Android中,您可以使用如下过滤器:

<?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>
your_button.Background.SetColorFilter(new Android.Graphics.PorterDuffColorFilter(Android.Graphics.Color.Red, Android.Graphics.PorterDuff.Mode.Multiply));

你有color.xml文件吗?@electrichead-是的,我有。我正在编辑我的答案。请看。我第一次看到这个答案是在我开始使用Android时,我被大量的XML代码吓坏了,但两个月后我又遇到了它,这是一个很好的答案,解决了创建自定义按钮颜色的一般问题,而不使用图像。这个答案比另一个答案更完整,谢谢!在我的例子中,我删除了项目android:color=“@color/black”s/b项目android:drawable=“@color/black”您现在可以在按钮属性中使用
style=“@style/Widget.AppCompat.Button.Colored”
!更多信息:后台应为getbackground()。另一个选项是使用这样的过滤器:myButton.getBackground().setColorFilter(新的LightingColorFilter(乘数,bckgrd_color));Konstantin的答案有一个输入错误-行
项android:color=“@color/black”
。。。。。。。。。。我建议它应该是项目android:drawable=“@color/black”。。。。。。。。。。。
<?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>
your_button.Background.SetColorFilter(new Android.Graphics.PorterDuffColorFilter(Android.Graphics.Color.Red, Android.Graphics.PorterDuff.Mode.Multiply));