声明
本文并非教程,而是笔记,不会对知识点一个一个的解释,当然如果能帮到小伙伴,我也会感到很欣慰,有好的意见以及方案,可以回复
目的
免除编写selector,很大程度的减少Xml文件,以及drawable的整洁,减少apk体积
可拓展性,其他特殊按钮继承CommonButton可以轻松拓展
方便全栈样式统一
知识点
GradientDrawable 对应XML
节点
StateListDrawable 对应XML节点
ColorStateList 对应XML color节点
GradientDrawable 以及XML比较
XML表示
android:color="@android:color/black"
android:dashWidth="1dp"
android:dashGap="2dp"/>
GradientDrawable 表示
View view = null; // 这个view是你需要设置背景的view
int strokeWidth = 1; // 1dp 边框宽度
int roundRadius = 5; // 5dp 圆角半径
int strokeColor = Color.parseColor("#FFFF0000");//边框颜色
int fillColor = Color.parseColor("#FF00FF00"); //内部填充颜色
GradientDrawable gd = new GradientDrawable();//创建drawable
gd.setColor(fillColor);
gd.setCornerRadius(roundRadius);
gd.setStroke(strokeWidth, strokeColor);
gd.setGradientType(GradientDrawable.RECTANGLE);
view.setBackgroundDrawable(gd);
// 创建渐变的shape drawable
int colors[] = { 0xff255779 , 0xff3e7492, 0xffa6c0cd };//分别为开始颜色,中间夜色,结束颜色
GradientDrawable gradientDrawable = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, colors);
view.setBackgroundDrawable(gd);
StateListDrawable以及XML比较
XML表示
StateListDrawable 表示
states = new int[4][];
//pressed, focused, normal, unable
states[0] = new int[] { android.R.attr.state_enabled, android.R.attr.state_pressed };
states[1] = new int[] { android.R.attr.state_enabled, android.R.attr.state_focused };
states[2] = new int[] { android.R.attr.state_enabled };
states[3] = new int[] { -android.R.attr.state_enabled};
StateListDrawable mStateBackground = new StateListDrawable();
//设置背景颜色
mStateBackground.addState(states[0], mPressedBackground);
mStateBackground.addState(states[1], mPressedBackground);
mStateBackground.addState(states[3], mUnableBackground);
mStateBackground.addState(states[2], mNormalBackground);
ColorStateList以及XML比较
XML表示
ColorStateList表示
//设置字体颜色
int[] colors = new int[] {mPressedTextColor, mPressedTextColor, mNormalTextColor, mUnableTextColor};
ColorStateList mTextColorStateList = new ColorStateList(states, colors);
掌握好这个几个知识点后写一个自定义Button就很简单了,配合之前常用的XML去理解 很好理解的
下面就是CommonButton的源代码 不到150行搞定一个基础的通用按钮,希望对你的项目有所帮助
CommonButton
/**
* 作者:小超 by Administrator on 2018/8/2 12:11
* 通用基础按钮
*/
public class CommonButton extends AppCompatButton {
//文字颜色 正常 点击 无效
private int mNormalTextColor;
private int mPressedTextColor;
private int mUnableTextColor;
//边框颜色 正常 点击 无效
private int mNormalStrokeColor;
private int mPressedStrokeColor;
private int mUnableStrokeColor;
//背景颜色 正常 点击 无效
private int mNormalBackgroundColor;
private int mPressedBackgroundColor;
private int mUnableBackgroundColor;
//圆角 左上 右上 左下 右下 mTopLeftRadius mTopRightRadius mBottomLeftRadius mBottomRightRadius 任何一个参数有值 mRadius将失效
private float mRadius;
private float mTopLeftRadius;
private float mTopRightRadius;
private float mBottomLeftRadius;
private float mBottomRightRadius;
//边框相关 间隔(虚线)宽度
private float mStrokeDashWidth;
private float mStrokeDashGap;
private int mNormalStrokeWidth;
private int mPressedStrokeWidth;
private int mUnableStrokeWidth;
private GradientDrawable mNormalBackground;
private GradientDrawable mPressedBackground;
private GradientDrawable mUnableBackground;
private int[][] states;
StateListDrawable mStateBackground;
ColorStateList mTextColorStateList;
public CommonButton(Context context) {
this(context, null);
}
public CommonButton(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CommonButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initAttrs(attrs);
initStyle();
}
private void initAttrs(AttributeSet attrs) {
TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.CommonButton);
mNormalTextColor = typedArray.getColor(R.styleable.CommonButton_normalTextColor, 0xffffffff);
mPressedTextColor = typedArray.getColor(R.styleable.CommonButton_pressedTextColor, 0xffffffff);
mUnableTextColor = typedArray.getColor(R.styleable.CommonButton_unableTextColor, 0xffffffff);
mNormalBackgroundColor = typedArray.getColor(R.styleable.CommonButton_normalBackgroundColor, 0xffff5053);
mPressedBackgroundColor = typedArray.getColor(R.styleable.CommonButton_pressedBackgroundColor, 0x80ff5053);
mUnableBackgroundColor = typedArray.getColor(R.styleable.CommonButton_unableBackgroundColor, 0x20000000);
mRadius = typedArray.getDimensionPixelSize(R.styleable.CommonButton_radius, 0);
mTopLeftRadius = typedArray.getDimensionPixelSize(R.styleable.CommonButton_topLeftRadius, 0);
mTopRightRadius = typedArray.getDimensionPixelSize(R.styleable.CommonButton_topRightRadius, 0);
mBottomLeftRadius = typedArray.getDimensionPixelSize(R.styleable.CommonButton_bottomLeftRadius, 0);
mBottomRightRadius = typedArray.getDimensionPixelSize(R.styleable.CommonButton_bottomRightRadius, 0);
mStrokeDashWidth = typedArray.getDimensionPixelSize(R.styleable.CommonButton_strokeDashWidth, 0);
mStrokeDashGap = typedArray.getDimensionPixelSize(R.styleable.CommonButton_strokeDashWidth, 0);
mNormalStrokeWidth = typedArray.getDimensionPixelSize(R.styleable.CommonButton_normalStrokeWidth, 0);
mPressedStrokeWidth = typedArray.getDimensionPixelSize(R.styleable.CommonButton_pressedStrokeWidth, 0);
mUnableStrokeWidth = typedArray.getDimensionPixelSize(R.styleable.CommonButton_unableStrokeWidth, 0);
mNormalStrokeColor = typedArray.getColor(R.styleable.CommonButton_normalStrokeColor, 0);
mPressedStrokeColor = typedArray.getColor(R.styleable.CommonButton_pressedStrokeColor, 0);
mUnableStrokeColor = typedArray.getColor(R.styleable.CommonButton_unableStrokeColor, 0);
typedArray.recycle();
}
private void initStyle() {
states = new int[4][];
//pressed, focused, normal, unable
states[0] = new int[] { android.R.attr.state_enabled, android.R.attr.state_pressed };
states[1] = new int[] { android.R.attr.state_enabled, android.R.attr.state_focused };
states[2] = new int[] { android.R.attr.state_enabled };
states[3] = new int[] { -android.R.attr.state_enabled};
Drawable drawable = getBackground();
if(drawable != null && drawable instanceof StateListDrawable){
mStateBackground = (StateListDrawable) drawable;
}else{
mStateBackground = new StateListDrawable();
}
mNormalBackground = new GradientDrawable();
mPressedBackground = new GradientDrawable();
mUnableBackground = new GradientDrawable();
mNormalBackground.setColor(mNormalBackgroundColor);
mPressedBackground.setColor(mPressedBackgroundColor);
mUnableBackground.setColor(mUnableBackgroundColor);
//设置圆角
if(mTopLeftRadius!=0||mTopRightRadius!=0||mBottomLeftRadius!=0||mBottomRightRadius!=0){
float[] radii={
mTopLeftRadius,mTopLeftRadius,
mTopRightRadius,mTopRightRadius,
mBottomRightRadius,mBottomRightRadius,
mBottomLeftRadius,mBottomLeftRadius,};
mNormalBackground.setCornerRadii(radii);
mPressedBackground.setCornerRadii(radii);
mUnableBackground.setCornerRadii(radii);
}else{
mNormalBackground.setCornerRadius(mRadius);
mPressedBackground.setCornerRadius(mRadius);
mUnableBackground.setCornerRadius(mRadius);
}
//设置边框
mNormalBackground.setStroke(mNormalStrokeWidth,mNormalStrokeColor,mStrokeDashWidth,mStrokeDashGap);
mPressedBackground.setStroke(mPressedStrokeWidth,mPressedStrokeColor,mStrokeDashWidth,mStrokeDashGap);
mUnableBackground.setStroke(mUnableStrokeWidth,mUnableStrokeColor,mStrokeDashWidth,mStrokeDashGap);
//设置字体颜色
int[] colors = new int[] {mPressedTextColor, mPressedTextColor, mNormalTextColor, mUnableTextColor};
mTextColorStateList = new ColorStateList(states, colors);
setTextColor(mTextColorStateList);
//设置背景颜色
mStateBackground.addState(states[0], mPressedBackground);
mStateBackground.addState(states[1], mPressedBackground);
mStateBackground.addState(states[3], mUnableBackground);
mStateBackground.addState(states[2], mNormalBackground);
setBackgroundDrawable(mStateBackground);
}
}
attrs