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

这篇博客讨论了如何更改标准Android按钮的颜色以适应品牌风格。作者探讨了使用drawable、颜色滤镜、自定义Button类、styles.xml以及第三方库等多种方法,并提供了代码示例和资源链接。读者可以了解到如何在不同情况下更简单、有效地调整Android按钮的颜色。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

到目前为止,我发现这样做的最好方法是将Button的drawable更改为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>

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

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


#1楼

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

首先,它没有提到在何处应用和清除彩色滤光片。 有可能还有其他好的地方可以做到这一点,但我想到的是一个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();
    }
});

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


#2楼

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

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


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

#4楼

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


#5楼

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


#6楼

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

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

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

普通按钮

正常图像

按下按钮

在此输入图像描述


#7楼

以这种方式使用它:

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

#8楼

我正在使用这种方法

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


#9楼

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

一个例子:

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

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


#10楼

您现在还可以将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"/>

#11楼

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

如果您使用标准灰色阴影按钮开头:

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

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

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

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

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


#12楼

这是我从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" />

自定义颜色的示例按钮


#13楼

我做一个非常好的按钮样式按钮的方式是子类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;
        }
    }

}

#14楼

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

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

#15楼

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

结果


#16楼

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

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

备注/要求

  • 使用app: namespace而不是 android: namespace!
  • appcompat版本> 24.2.0

    依赖{compile'c​​om.android.support:appcompat-v7:25.3.1'}

说明在此输入图像描述


#17楼

根据材料设计指南,您需要使用如下代码的样式

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

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

    android:theme="@style/MyButton"

#18楼

它很简单..在你的项目中添加这个依赖项并创建一个带有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" />

#19楼

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


#20楼

如果您使用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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值