我想稍微改变一个标准的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。

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


当前回答

我喜欢@conjugatedirection和@Tomasz之前回答中的颜色滤镜建议;然而,我发现到目前为止提供的代码并不像我预期的那样容易应用。

首先,它没有提到在哪里应用和清除颜色滤镜。可能还有其他好地方可以做到这一点,但我想到的是OnTouchListener。

From my reading of the original question, the ideal solution would be one that does not involve any images. The accepted answer using custom_button.xml from @emmby is probably a better fit than color filters if that's your goal. In my case, I'm starting with a png image from a UI designer of what the button is supposed to look like. If I set the button background to this image, the default highlight feedback is lost completely. This code replaces that behavior with a programmatic darkening effect.

button.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                // 0x6D6D6D sets how much to darken - tweak as desired
                setColorFilter(v, 0x6D6D6D);
                break;
            // remove the filter when moving off the button
            // the same way a selector implementation would 
            case MotionEvent.ACTION_MOVE:
                Rect r = new Rect();
                v.getLocalVisibleRect(r);
                if (!r.contains((int) event.getX(), (int) event.getY())) {
                    setColorFilter(v, null);
                }
                break;
            case MotionEvent.ACTION_OUTSIDE:
            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_UP:
                setColorFilter(v, null);
                break;
        }
        return false;
    }

    private void setColorFilter(View v, Integer filter) {
        if (filter == null) v.getBackground().clearColorFilter();
        else {
            // To lighten instead of darken, try this:
            // LightingColorFilter lighten = new LightingColorFilter(0xFFFFFF, filter);
            LightingColorFilter darken = new LightingColorFilter(filter, 0x000000);
            v.getBackground().setColorFilter(darken);
        }
        // required on Android 2.3.7 for filter change to take effect (but not on 4.0.4)
        v.getBackground().invalidateSelf();
    }
});

我将其提取为一个单独的类,用于应用到多个按钮-显示为匿名内部类只是为了了解这个概念。

其他回答

DroidUX组件库有一个ColorButton小部件,它的颜色可以很容易地通过xml定义和运行时以编程方式改变,所以你甚至可以让用户设置按钮的颜色/主题,如果你的应用程序允许的话。

我正在使用这种方法

style.xml

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:colorPrimaryDark">#413152</item>
    <item name="android:colorPrimary">#534364</item>
    <item name="android:colorAccent">#534364</item>
    <item name="android:buttonStyle">@style/MyButtonStyle</item>
</style>

<style name="MyButtonStyle" parent="Widget.AppCompat.Button.Colored">
    <item name="android:colorButtonNormal">#534364</item>
    <item name="android:textColor">#ffffff</item>
</style>

正如你从上面看到的,我正在为我的按钮使用自定义样式。按钮颜色与强调色相对应。我发现这是一个比设置android:background更好的方法,因为我不会失去谷歌提供的涟漪效应。

在<Button>中使用android:background="#33b5e5"。或者更好的android:background="@color/navig_button"

这是我的解决方案,从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" />

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

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

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

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

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

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

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