Standard Android Button with a different color

后端 未结 20 3304
误落风尘
误落风尘 2020-11-21 23:43

I\'d like to change the color of a standard Android button slightly in order to better match a client\'s branding.

The best way I\'ve found to do this so far is to c

20条回答
  •  误落风尘
    2020-11-22 00:06

    The way I do a different styled button that works quite well is to subclass the Button object and apply a colour filter. This also handles enabled and disabled states by applying an alpha to the button.

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

提交回复
热议问题