72

我正在尝试在我的应用程序中设置按钮的背景颜色,但我无法达到我想要的结果......

我要设置的颜色是holo_green_light(#ff99cc00). 为了做到这一点,我正在使用setColorFilter(0xff99cc00, PorterDuff.Mode.MULTIPLY);

我得到的颜色不是浅灰色和浅灰色holo_green_light的混合holo_green_light

我试过使用LightingColorFilter没有太大的成功。

有没有办法以编程方式做到这一点,使按钮看起来像一个按钮,而不是我需要的颜色的平面矩形。

4

11 回答 11

65

如果您想保持一般样式(圆角等)并且只更改背景颜色,那么我使用 backgroundTint 属性

android:backgroundTint="@android:color/holo_green_light"
于 2017-11-16T10:30:31.587 回答
48

这是我用不同颜色做自定义按钮的方法:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <stroke android:width="3dp"
        android:color="#80FFFFFF" />
 
    <corners android:radius="25dp" />
 
    <gradient android:angle="270"
            android:centerColor="#90150517"
            android:endColor="#90150517"
            android:startColor="#90150517" />
</shape>

这样你设置为背景:

<Button android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        android:layout_marginBottom="25dp"
        android:layout_centerInParent="true"
        android:background="@drawable/button" />
于 2013-08-07T09:45:27.533 回答
18

如果您不介意对其进行硬编码,您可以这样做 ~> android:background="#eeeeee" 并删除您希望的任何十六进制颜色#。

看起来像这样......

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/textView1"
    android:text="@string/ClickMe"
    android:background="#fff"/>
于 2014-07-17T14:04:16.663 回答
15

/res/drawable/button.xml使用以下内容创建 :

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:padding="10dp">
<!-- you can use any color you want I used here gray color-->
 <solid android:color="#90EE90"/> 
    <corners
     android:bottomRightRadius="3dp"
     android:bottomLeftRadius="3dp"
  android:topLeftRadius="3dp"
  android:topRightRadius="3dp"/>
</shape>

然后您可以使用以下内容:

<Button
    android:id="@+id/button_save_prefs"
    android:text="@string/save"
    android:background="@drawable/button"/>
于 2013-08-06T01:04:27.593 回答
10

只需使用 aMaterialButtonapp:backgroundTint属性:

<MaterialButton
  app:backgroundTint="@color/my_color_selector"

在此处输入图像描述

于 2020-05-26T07:13:42.190 回答
5

为什么不直接使用setBackgroundColor(getResources().getColor(R.color.holo_light_green))

编辑:如果你想要一个看起来更像 Android 按钮的东西,你会想要创建一个渐变并将其设置为背景。例如,您可以查看这个问题

于 2013-08-06T00:41:47.997 回答
5

没必要那么铁杆。
尝试这个 :

YourButtonObject.setBackground(0xff99cc00);
于 2015-08-11T23:39:22.690 回答
3

尝试这个

<androidx.appcompat.widget.AppCompatButton
    android:layout_width="wrap_content"
    android:layout_height="34dp"
    android:text="Check Out"
    android:textAllCaps="true"
    android:background="#54c2bc"
    android:textColor="#FFFFFF"
    android:textSize="9sp"/>
于 2021-01-26T12:25:27.900 回答
1

为了保持风格,请使用:

int color = Color.parseColor("#99cc00");
button.getBackground().mutate().setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.SRC));
于 2018-01-08T20:55:23.883 回答
0

除了马克普罗克特的回答:

如果您想保留默认样式,但在按钮上有条件着色,只需backgroundTint像这样设置属性:

android:backgroundTint="@drawable/styles_mybutton"

创建相关文件 /res/drawable/styles_mybutton.xml,然后使用以下模板并根据您的喜好更改颜色:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Disabled state-->
    <item android:state_enabled="false"
        android:color="@android:color/white">
    </item>
    <!-- Default state-->
    <item
        android:color="#cfc">
    </item>
</selector>
于 2018-07-23T12:53:44.913 回答
-1

使用材料设计库1.2.0-alpha06版本,现在我们可以在组件上使用:android:background="..."MaterialButton

<com.google.android.material.button.MaterialButton
    android:background="#fff"
    ...
/>
于 2020-05-23T08:02:50.153 回答