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

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


当前回答

其简单. .将此依赖项添加到项目中,并使用创建按钮 1. 任何形状 2. 任何颜色 3.任何边界 4. 有物质效应

https://github.com/manojbhadane/QButton

<com.manojbhadane.QButton
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:text="OK"
       app:qb_backgroundColor="@color/green"
       app:qb_radius="100"
       app:qb_strokeColor="@color/darkGreen"
       app:qb_strokeWidth="5" />

其他回答

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

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

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

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

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

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

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

我喜欢@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();
    }
});

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

根据材料设计指南,你需要使用喜欢的风格 下面的代码

<style name="MyButton" parent="Theme.AppCompat.Light>
    <item name="colorControlHighlight">#F36F21</item>
    <item name="colorControlHighlight">#FF8D00</item>
</style>

在布局中将此属性添加到按钮中

    android:theme="@style/MyButton"

我正在使用这种方法

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更好的方法,因为我不会失去谷歌提供的涟漪效应。

这样使用它:

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