Button动态样式取代xml

本文介绍了一个自定义Button组件的方法,支持动态设置背景色、边框、圆角等样式,无需XML配置,简化了UI开发流程。

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

转载标明出处:http://www.cnblogs.com/no-coding

还在为 textview以及button 的各种样式而烦恼的童鞋们请往这里看~~~~

一次性解决 textview以及button的样式,再也不用写xml了!!!

全部动态预设置,拒绝堆代码,拒绝xml...

支持的样式如下

1
2
3
4
5
6
7
8
9
10
11
private int backColor = 0;//背景色
  private int backColorSelected = 0;//按下后的背景色
  private int backGroundImage = 0;//背景图
  private int backGroundImageSeleted = 0;//按下后的背景图
  private int textColor = Color.BLACK;//文字颜色
  private int textColorSeleted = 0;//按下后的文字颜色
  private float radius = 8;//圆角半径
  private int shape = 0;//圆角样式,矩形、圆形等,由于矩形的Id为0,默认为矩形
  private Boolean fillet = false;//是否设置圆角
  private int strokeWidth = 1;//边框宽度
  private int strokeColor = Color.TRANSPARENT;//边框颜色

  

自定义属性:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<declare-styleable name="GCButton">
        <attr name="backColors" format="color"/>
        <attr name="backColorSelected" format="color"/>
        <attr name="backGroundImage" format="reference"/>
        <attr name="backGroundImageSeleted" format="reference"/>
        <attr name="textColorSeleted" format="color"/>
        <attr name="radius" format="dimension"/>
        <attr name="shape">
            <enum name="RECTANGLE" value="0"/>
            <enum name="OVAL" value="1"/>
            <enum name="LINE" value="2"/>
            <enum name="RING" value="3"/>
        </attr>
        <attr name="fillet" format="boolean"/>
        <attr name="strokeWidth" format="dimension"/>
        <attr name="strokeColor" format="color"/>
    </declare-styleable>

  完整源码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
 
@SuppressLint("AppCompatCustomView")
public class GCButton extends Button {
    private Context mContext;
    private GradientDrawable gradientDrawable;
    private int backColor = 0;//背景色
    private int backColorSelected = 0;//按下后的背景色
    private int backGroundImage = 0;//背景图
    private int backGroundImageSeleted = 0;//按下后的背景图
    private int textColor = Color.BLACK;//文字颜色
    private int textColorSeleted = 0;//按下后的文字颜色
    private float radius = 8;//圆角半径
    private int shape = 0;//圆角样式,矩形、圆形等,由于矩形的Id为0,默认为矩形
    private Boolean fillet = false;//是否设置圆角
    private int strokeWidth = 1;//边框宽度
    private int strokeColor = Color.TRANSPARENT;//边框颜色
 
    public GCButton(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.mContext = context;
        init();
        initAttrs(attrs);
    }
 
    public GCButton(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }
 
    public GCButton(Context context) {
        this(context, null);
    }
 
    private void initAttrs(AttributeSet attrs) {
        TypedArray a = mContext.obtainStyledAttributes(attrs, R.styleable.GCButton);
        setStroke(a.getDimensionPixelSize(R.styleable.GCButton_strokeWidth, strokeWidth), a.getColor(R.styleable
                .GCButton_strokeColor, strokeColor));
        setBackColor(a.getColor(R.styleable.GCButton_backColors, 0));
        setBackColorSelected(a.getColor(R.styleable.GCButton_backColorSelected, 0));
        setBackGroundImage(a.getInteger(R.styleable.GCButton_backGroundImage, 0));
        setBackGroundImageSeleted(a.getInteger(R.styleable.GCButton_backGroundImageSeleted, 0));
        setTextColorSelected(a.getColor(R.styleable.GCButton_textColorSeleted, 0));
        setRadius(a.getDimensionPixelSize(R.styleable.GCButton_radius, 0));
        setShape(a.getInt(R.styleable.GCButton_shape, 0));
        setFillet(a.getBoolean(R.styleable.GCButton_fillet, false));
        a.recycle();
        TypedArray b = mContext.obtainStyledAttributes(attrs, new int[]{android.R.attr.textColor});
        setTextColor(b.getColor(0, textColor));
        b.recycle();
        invalidateView();
    }
 
    @Override
    public void setOnClickListener(@Nullable final OnClickListener l) {
        super.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                l.onClick(v);
            }
        });
    }
 
    private void init() {
        //将Button的默认背景色改为透明
//        if (fillet) {
//            if (gradientDrawable == null) {
//                gradientDrawable = new GradientDrawable();
//            }
//            gradientDrawable.setColor(Color.TRANSPARENT);
//        } else {
//            setBackgroundColor(Color.TRANSPARENT);
//        }
        //设置文字默认居中
        setGravity(Gravity.CENTER);
        //设置Touch事件
        setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View arg0, MotionEvent event) {
                //按下改变样式
                setColor(event.getAction());
                //此处设置为false,防止Click事件被屏蔽
                return false;
            }
        });
        setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
 
            }
        });
    }
 
    //改变样式
    @SuppressWarnings("Range")
    private void setColor(int state) {
        if (state == MotionEvent.ACTION_DOWN) {
            //按下
            if (backColorSelected != 0) {
                //先判断是否设置了按下后的背景色int型
                if (fillet) {
                    if (gradientDrawable == null) {
                        gradientDrawable = new GradientDrawable();
                    }
                    gradientDrawable.setColor(backColorSelected);
                else {
                    setBackgroundColor(backColorSelected);
                }
            else if (backColorSelected != 0) {
                if (fillet) {
                    if (gradientDrawable == null) {
                        gradientDrawable = new GradientDrawable();
                    }
                    gradientDrawable.setColor(backColorSelected);
                else {
                    setBackgroundColor(backColorSelected);
                }
            }
            //判断是否设置了按下后文字的颜色
            if (textColorSeleted != 0) {
                setTextColor(textColorSeleted);
            else if (backColorSelected != 0) {
                setTextColor(backColorSelected);
            }
            //判断是否设置了按下后的背景图
            if (backGroundImageSeleted != 0) {
                setBackgroundResource(backGroundImageSeleted);
            }
        }
        if (state == MotionEvent.ACTION_UP) {
            //抬起
            if (backColor == 0) {
                //如果没有设置背景色,默认改为透明
                if (fillet) {
                    if (gradientDrawable == null) {
                        gradientDrawable = new GradientDrawable();
                    }
                    gradientDrawable.setColor(Color.TRANSPARENT);
                else {
                    setBackgroundColor(Color.TRANSPARENT);
                }
            else if (backColor != 0) {
                if (fillet) {
                    if (gradientDrawable == null) {
                        gradientDrawable = new GradientDrawable();
                    }
                    gradientDrawable.setColor(backColor);
                else {
                    setBackgroundColor(backColor);
                }
            else {
                if (fillet) {
                    if (gradientDrawable == null) {
                        gradientDrawable = new GradientDrawable();
                    }
                    gradientDrawable.setColor(backColor);
                else {
                    setBackgroundColor(backColor);
                }
            }
            //如果为设置字体颜色,默认为黑色
            if (textColor == 0) {
                setTextColor(Color.BLACK);
            else if (textColor != 0) {
                setTextColor(textColor);
            else {
                setTextColor(textColor);
            }
            if (backGroundImage != 0) {
                setBackgroundResource(backGroundImage);
            }
        }
    }
 
    /**
     * 设置按钮的背景色,如果未设置则默认为透明
     *
     * @param backColor
     */
    public void setBackColor(int backColor) {
        this.backColor = backColor;
    }
 
    /**
     * 设置按钮按下后的颜色
     *
     * @param backColorSelected
     */
    public void setBackColorSelected(int backColorSelected) {
        this.backColorSelected = backColorSelected;
    }
 
 
    /**
     * 设置按钮的背景图
     *
     * @param backGroundImage
     */
    public void setBackGroundImage(int backGroundImage) {
        this.backGroundImage = backGroundImage;
    }
 
    /**
     * 设置按钮按下的背景图
     *
     * @param backGroundImageSeleted
     */
    public void setBackGroundImageSeleted(int backGroundImageSeleted) {
        this.backGroundImageSeleted = backGroundImageSeleted;
    }
 
    /**
     * 设置按钮圆角半径大小
     *
     * @param radius
     */
    public void setRadius(float radius) {
        this.radius = radius;
    }
 
 
    /**
     * 设置按钮文字颜色
     *
     * @param textColor
     */
    public void settextColor(int textColor) {
        this.textColor = textColor;
        setTextColor(textColor);
    }
 
 
    /**
     * 设置按钮按下的文字颜色
     *
     * @param textColor
     */
    public void setTextColorSelected(int textColor) {
        this.textColorSeleted = textColor;
    }
 
    /**
     * 按钮的形状
     *
     * @param shape
     */
    public void setShape(int shape) {
        this.shape = shape;
    }
 
    /**
     * 设置其是否为圆角
     *
     * @param fillet
     */
    @SuppressWarnings("deprecation")
    public void setFillet(Boolean fillet) {
        this.fillet = fillet;
    }
 
    public void setStroke(int strokeWidth, int strokeColor) {
        this.strokeWidth = strokeWidth;
        this.strokeColor = strokeColor;
    }
 
    public void invalidateView() {
        if (gradientDrawable == null) {
            gradientDrawable = new GradientDrawable();
        }
        if (backColor == 0) {
            if (fillet) {
                if (gradientDrawable == null) {
                    gradientDrawable = new GradientDrawable();
                }
                gradientDrawable.setColor(Color.TRANSPARENT);
            else {
                setBackgroundColor(Color.TRANSPARENT);
            }
        else {
            if (fillet) {
                if (gradientDrawable == null) {
                    gradientDrawable = new GradientDrawable();
                }
                gradientDrawable.setColor(backColor);
            else {
                setBackgroundColor(backColor);
            }
        }
        if (backGroundImage != 0) {
            setBackgroundResource(backGroundImage);
        }
        //GradientDrawable.RECTANGLE
        if (fillet) {
            gradientDrawable.setShape(shape);
            gradientDrawable.setCornerRadius(radius);
            gradientDrawable.setStroke(strokeWidth, strokeColor);
            setBackgroundDrawable(gradientDrawable);
        }
    }
 
    public abstract class NoDoubleClickListener implements OnClickListener {
 
        public static final int MIN_CLICK_DELAY_TIME = 1000;
        private long lastClickTime = 0;
 
        @Override
        public void onClick(View v) {
            long currentTime = System.currentTimeMillis();
            if (currentTime - lastClickTime > MIN_CLICK_DELAY_TIME) {
                lastClickTime = currentTime;
                onNoDoubleClick(v);
            }
        }
 
        protected abstract void onNoDoubleClick(View v);
    }
}

  

需要设置边框颜色,形状等 fillet 属性设置为true

 

后续会增加阴影等更多效果~~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值