首页 文章

标准Android按钮具有不同的颜色

提问于
浏览
700

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

到目前为止,我发现这样做的最好方法是将 Button 的drawable更改为 res/drawable/red_button.xml 中的drawable:

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

但这样做需要我实际为每个我想要自定义的按钮创建三个不同的drawable(一个用于静止按钮,一个用于聚焦,一个用于按下) . 这似乎比我需要的更复杂,更干燥 .

我真正想做的就是对按钮应用某种颜色转换 . 是否有更简单的方法来改变按钮的颜色而不是我正在做的事情?

18 回答

  • 145

    迈克,您可能对彩色滤光片感兴趣 .

    一个例子:

    button.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0xFFAA0000));
    

    试试这个来达到你想要的颜色 .

  • 10

    我正在使用这种方法

    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>
    

    从上面可以看出,我正在为我的按钮使用自定义样式 . 按钮颜色对应于强调颜色 . 我发现这比设置_348257更好,因为我不会失去Google提供的连锁反应 .

  • 0

    以这种方式使用它:

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

    我发现这一切都可以很容易地在一个文件中完成 . 将类似下面的代码放在名为 custom_button.xml 的文件中,然后在按钮视图中设置 background="@drawable/custom_button"

    <?xml version="1.0" encoding="utf-8"?>
    <selector
        xmlns:android="http://schemas.android.com/apk/res/android">
    
        <item android:state_pressed="true" >
            <shape>
                <gradient
                    android:startColor="@color/yellow1"
                    android:endColor="@color/yellow2"
                    android:angle="270" />
                <stroke
                    android:width="3dp"
                    android:color="@color/grey05" />
                <corners
                    android:radius="3dp" />
                <padding
                    android:left="10dp"
                    android:top="10dp"
                    android:right="10dp"
                    android:bottom="10dp" />
            </shape>
        </item>
    
        <item android:state_focused="true" >
            <shape>
                <gradient
                    android:endColor="@color/orange4"
                    android:startColor="@color/orange5"
                    android:angle="270" />
                <stroke
                    android:width="3dp"
                    android:color="@color/grey05" />
                <corners
                    android:radius="3dp" />
                <padding
                    android:left="10dp"
                    android:top="10dp"
                    android:right="10dp"
                    android:bottom="10dp" />
            </shape>
        </item>
    
        <item>        
            <shape>
                <gradient
                    android:endColor="@color/blue2"
                    android:startColor="@color/blue25"
                    android:angle="270" />
                <stroke
                    android:width="3dp"
                    android:color="@color/grey05" />
                <corners
                    android:radius="3dp" />
                <padding
                    android:left="10dp"
                    android:top="10dp"
                    android:right="10dp"
                    android:bottom="10dp" />
            </shape>
        </item>
    </selector>
    
  • 302

    您可以将按钮的主题设置为此

    <style name="AppTheme.ButtonBlue" parent="Widget.AppCompat.Button.Colored">
     <item name="colorButtonNormal">@color/HEXColor</item>
     <item name="android:textColor">@color/HEXColor</item>
    </style>
    
  • 9

    一种简单的方法是只定义一个自定义Button类,它接受所需的所有属性,如半径,渐变,按下的颜色,正常颜色等,然后在XML布局中使用它而不是使用XML设置背景 . 样本是here

    如果您有许多具有相同属性(如半径,选定颜色等)的按钮,则此功能非常有用 . 您可以自定义继承按钮处理这些额外的属性 .

    结果(未使用背景选择器) .

    Normal Button

    Normal Image

    Pressed Button

    enter image description here

  • 8

    您还可以使用此在线工具自定义按钮http://angrytools.com/android/button/并使用 android:background="@drawable/custom_btn" 在布局中定义自定义按钮 .

  • 0

    我喜欢@conjugatedirection和@Tomasz先前答案中的滤色器建议;但是,我发现到目前为止提供的代码并不像我预期的那样容易应用 .

    首先,没有提到 where 来应用和清除滤色器 . 有可能还有其他好的地方可以做到这一点,但我想到的是OnTouchListener .

    从我对原始问题的解读,理想的解决方案将是不涉及任何图像的解决方案 . 如果这是你的目标,使用@emmby中的custom_button.xml接受的答案可能比彩色滤镜更合适 . 在我的例子中,我从一个UI设计师的png图像开始,按钮应该是什么样子 . 如果我将按钮背景设置为此图像,则默认的高光反馈将完全丢失 . 此代码使用程序化变暗效果替换该行为 .

    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();
        }
    });
    

    我把它作为一个单独的类提取出来,用于应用于多个按钮 - 显示为匿名内部类只是为了得到这个想法 .

  • 4

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

    如果您从标准灰色阴影按钮开始:

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

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

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

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

    它的工作原理是将当前按钮颜色值乘以颜色值 . 我相信你可以用这些模式做更多的事情 .

  • 8

    如果要使用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>
    
  • 51

    我做一个非常好的按钮样式按钮的方式是子类Button对象并应用颜色过滤器 . 这也通过对按钮应用alpha来处理启用和禁用状态 .

    import android.annotation.TargetApi;
    import android.content.Context;
    import android.graphics.Color;
    import android.graphics.ColorFilter;
    import android.graphics.LightingColorFilter;
    import android.graphics.drawable.Drawable;
    import android.graphics.drawable.LayerDrawable;
    import android.os.Build;
    import android.util.AttributeSet;
    import android.widget.Button;
    
    public class DimmableButton extends Button {
    
        public DimmableButton(Context context) {
            super(context);
        }
    
        public DimmableButton(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public DimmableButton(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }
    
        @SuppressWarnings("deprecation")
        @Override
        public void setBackgroundDrawable(Drawable d) {
            // Replace the original background drawable (e.g. image) with a LayerDrawable that
            // contains the original drawable.
            DimmableButtonBackgroundDrawable layer = new DimmableButtonBackgroundDrawable(d);
            super.setBackgroundDrawable(layer);
        }
    
        @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
        @Override
        public void setBackground(Drawable d) {
            // Replace the original background drawable (e.g. image) with a LayerDrawable that
            // contains the original drawable.
            DimmableButtonBackgroundDrawable layer = new DimmableButtonBackgroundDrawable(d);
            super.setBackground(layer);
        }
    
        /**
         * The stateful LayerDrawable used by this button.
         */
        protected class DimmableButtonBackgroundDrawable extends LayerDrawable {
    
            // The color filter to apply when the button is pressed
            protected ColorFilter _pressedFilter = new LightingColorFilter(Color.LTGRAY, 1);
            // Alpha value when the button is disabled
            protected int _disabledAlpha = 100;
            // Alpha value when the button is enabled
            protected int _fullAlpha = 255;
    
            public DimmableButtonBackgroundDrawable(Drawable d) {
                super(new Drawable[] { d });
            }
    
            @Override
            protected boolean onStateChange(int[] states) {
                boolean enabled = false;
                boolean pressed = false;
    
                for (int state : states) {
                    if (state == android.R.attr.state_enabled)
                        enabled = true;
                    else if (state == android.R.attr.state_pressed)
                        pressed = true;
                }
    
                mutate();
                if (enabled && pressed) {
                    setColorFilter(_pressedFilter);
                } else if (!enabled) {
                    setColorFilter(null);
                    setAlpha(_disabledAlpha);
                } else {
                    setColorFilter(null);
                    setAlpha(_fullAlpha);
                }
    
                invalidateSelf();
    
                return super.onStateChange(states);
            }
    
            @Override
            public boolean isStateful() {
                return true;
            }
        }
    
    }
    
  • 2

    这是我的解决方案,完美起作用 from API 15 . 此解决方案保留所有默认按钮单击效果,如材料 RippleEffect . 我没有在较低的API上测试它,但它应该工作 .

    您需要做的就是:

    1)创建仅更改 colorAccent 的样式:

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

    我建议使用ThemeOverlay.AppCompat或您的主AppTheme作为父级,以保留其余的样式 .

    2)将这两行添加到 button 小部件:

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

    有时您的新colorAccent未在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" />
    

    Sample Button with custom color

  • 78

    DroidUX组件库有一个ColorButton小部件,其颜色可以通过xml定义和运行时以编程方式轻松更改,因此您甚至可以让用户在应用程序允许的情况下设置按钮的颜色/主题 .

  • 22

    现在有一种更简单的方法:android-holo-colors.com

    它可以让你轻松改变所有holo drawables(纽扣,旋转器......)的颜色 . 您选择颜色,然后下载包含所有分辨率的drawable的zip文件 .

  • 16

    The shortest solution which is working with any Android version:

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

    Notes/Requirements

    • 使用 app: 命名空间和 not android: 命名空间!

    • appcompat版本> 24.2.0

    依赖{compile'com.android.support:appcompat-v7:25.3.1'}

    Explanation
    enter image description here

  • 1

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

  • 705

    值\ styles.xml

    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
    
    <style name="RedAccentButton" parent="ThemeOverlay.AppCompat.Light">
        <item name="colorAccent">#ff0000</item>
    </style>
    

    然后:

    <Button
        style="@style/Widget.AppCompat.Button.Colored"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="text" />
    
    <Button
        style="@style/Widget.AppCompat.Button.Colored"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:enabled="false"
        android:text="text" />
    
    <Button
        style="@style/Widget.AppCompat.Button.Colored"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="text"
        android:theme="@style/RedAccentButton" />
    
    <Button
        style="@style/Widget.AppCompat.Button.Colored"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:enabled="false"
        android:text="text"
        android:theme="@style/RedAccentButton" />
    

    result

  • 3

    您现在还可以将appcompat-v7的AppCompatButtonbackgroundTint 属性一起使用:

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

相关问题