安卓按钮背景颜色

87

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

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

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

我尝试使用LightingColorFilter没有取得太大的成功。

有没有一种编程的方法,使得按钮看起来像一个按钮,而不是一个带有所需颜色的平面矩形。


尝试使用android:background="@drawable/button"。 - Giant
14个回答

91

如果您想保留一般的样式(如圆角等),只是更改背景颜色,则可以使用 backgroundTint 属性

android:backgroundTint="@android:color/holo_green_light"

16
android:backgroundTint 只能在API版本大于等于21时使用。请改用 app:backgroundTint - repitch
1
@repitch app:backgroundTint 对所有的 API 都适用吗? - John Joe
在Proctor和Repitch之间,这应该是这个问题最常见的答案。 - C. Skjerdal

50

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

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

14
对于像我这样的新手,如果您能更明确地指出第一个代码示例应该放在哪里,那将会很有帮助。它是应该放在“drawables”文件夹里名为“Button.xml”的文件中吗? - Zefira
2
嗨,Geoff。请在res文件夹中创建一个drawable文件夹并创建一个button.xml文件。请复制粘贴此代码,并将此xml命名为您的按钮背景。它会起作用的。 - Bebin T.N

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

8
仅仅更改背景颜色的话,你会失去圆角和默认选中状态。 - eliasbagley

16

创建 /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"/>

我不得不将角落半径设置为13dp才能使其可见;谢谢! - Phlip

16

只需使用MaterialButtonapp:backgroundTint属性:

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

在此输入图片描述


6

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

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


1
那样做会让按钮看起来像一个平面矩形吗? - user2260040
默认的Android按钮看起来具有渐变背景,因此如果您想模仿那种效果,您可能需要自己创建一个渐变并将其设置为背景。我添加了一条说明。 - Jonathan

5

试一试

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

5
不必那么极端。
试试这个:
YourButtonObject.setBackground(0xff99cc00);

1
为了保持样式,请使用:

int color = Color.parseColor("#99cc00");
button.getBackground().mutate().setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.SRC));

为什么你添加了 mutate? - Karan Harsh Wardhan

1

试试这个

app:backgroundTint="@color/colorGreen"


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接