我想稍微改变一个标准的Android按钮的颜色,以便更好地匹配客户端的品牌。

到目前为止,我发现最好的方法是将按钮的可绘制对象更改为位于res/drawable/red_button.xml中的可绘制对象:

<?xml version="1.0" encoding="utf-8"?>    
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@drawable/red_button_pressed" />
    <item android:state_focused="true" android:drawable="@drawable/red_button_focus" />
    <item android:drawable="@drawable/red_button_rest" />
</selector>

但是这样做需要为我想要自定义的每个按钮创建三个不同的绘图(一个用于静止的按钮,一个用于聚焦时的按钮,一个用于按下时的按钮)。这似乎比我需要的更复杂和非dry。

我真正想做的是对按钮应用某种颜色变换。有没有比我现在做的更简单的方法来改变按钮的颜色?


当前回答

最短的解决方案,适用于任何Android版本:

<Button
     app:backgroundTint="@color/my_color"

Notes /要求:

使用app: namespace而不是android: namespace! Appcompat版本> 24.2.0 依赖关系{ 编译“com.android.support: appcompat-v7:25.3.1” }

解释:

其他回答

这是我的解决方案,从API 15开始完美地工作。这个解决方案保留了所有默认的按钮点击效果,如材质涟漪效应。我没有在较低的api上测试它,但它应该可以工作。

你所需要做的就是:

1)创建一个只改变颜色口音的样式:

<style name="Facebook.Button" parent="ThemeOverlay.AppCompat">
    <item name="colorAccent">@color/com_facebook_blue</item>
</style>

我推荐使用ThemeOverlay。AppCompat或你的主AppTheme作为父,以保留你的其他样式。

2)添加这两行到你的按钮小部件:

style="@style/Widget.AppCompat.Button.Colored"
android:theme="@style/Facebook.Button"

有时你的新颜色口音不会在Android Studio预览中显示,但当你在手机上启动应用程序时,颜色会发生变化。


示例按钮小部件

<Button
    android:id="@+id/sign_in_with_facebook"
    style="@style/Widget.AppCompat.Button.Colored"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="@string/sign_in_facebook"
    android:textColor="@android:color/white"
    android:theme="@style/Facebook.Button" />

你现在也可以使用appcompat-v7的appcompat按钮和backgroundTint属性:

<android.support.v7.widget.AppCompatButton
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:backgroundTint="#ffaa00"/>

如果您使用XML制作彩色按钮,您可以在单独的文件中指定聚焦和按下状态并重用它们,从而使代码更加清晰。我的绿色按钮是这样的:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_focused="true" android:drawable="@drawable/button_focused"/>
    <item android:state_pressed="true" android:drawable="@drawable/button_pressed"/>

    <item>
        <shape>
            <gradient android:startColor="#ff00ff00" android:endColor="#bb00ff00" android:angle="270" />
            <stroke android:width="1dp" android:color="#bb00ff00" />
            <corners android:radius="3dp" />
            <padding android:left="10dp" android:top="10dp" android:right="10dp" android:bottom="10dp" />
        </shape>
    </item>

</selector>

这样使用它:

buttonOBJ.getBackground().setColorFilter(Color.parseColor("#YOUR_HEX_COLOR_CODE"), PorterDuff.Mode.MULTIPLY);

根据Tomasz的回答,您还可以使用PorterDuff乘法模式以编程方式设置整个按钮的阴影。这将改变按钮的颜色,而不仅仅是色调。

如果你从一个标准的灰色阴影按钮开始:

button.getBackground().setColorFilter(0xFFFF0000, PorterDuff.Mode.MULTIPLY);

会给你一个红色阴影按钮,

button.getBackground().setColorFilter(0xFF00FF00, PorterDuff.Mode.MULTIPLY);

会给你一个绿色阴影按钮等,其中第一个值是十六进制格式的颜色。

它通过将当前按钮颜色值乘以您的颜色值来工作。我相信你还可以用这些模式做更多的事情。