我创建了一个自定义视图,它扩展了按钮,并根据我提供的颜色创建了一个简单的Drawable背景。
未指定填充结果没有填充。 在单个FlatButton的XML声明中指定填充会导致无填充。 以编程方式设置填充没有任何影响。
是什么给出了?
public class FlatButton extends Button {
private int buttonColor;
private int pressedButtonColor = -1;
private int cornerRadius;
private int paddingLeft;
private int paddingTop;
private int paddingRight;
private int paddingBottom;
private Drawable pressedDrawable;
private Drawable unpressedDrawable;
public FlatButton(Context context) {
super(context);
init();
}
public FlatButton(Context context, AttributeSet attrs) {
super(context, attrs);
init();
parseAttrs(context, attrs);
}
public FlatButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
parseAttrs(context, attrs);
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
refresh();
}
public void refresh() {
int alpha = Color.alpha(buttonColor);
float[] hsv = new float[3];
Color.colorToHSV(buttonColor, hsv);
hsv[2] *= 0.8f;
//if shadow color was not defined, generate shadow color = 120% brightness
if (pressedButtonColor == -1) {
pressedButtonColor = Color.HSVToColor(alpha, hsv);
}
//Create pressed background and unpressed background drawables
if (this.isEnabled()) {
pressedDrawable = createDrawable(cornerRadius, pressedButtonColor);
unpressedDrawable = createDrawable(cornerRadius, buttonColor);
} else {
Color.colorToHSV(buttonColor, hsv);
hsv[1] *= 0.25f; // saturation component
int disabledColor = Color.HSVToColor(alpha, hsv);
// Disabled button does not have shadow
pressedDrawable = createDrawable(cornerRadius, disabledColor);
unpressedDrawable = createDrawable(cornerRadius, disabledColor);
}
Drawable drawable = createStateListDrawable(pressedDrawable, unpressedDrawable);
updateBackground(drawable);
setPadding();
}
private void setPadding() {
this.setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom);
}
private Drawable createStateListDrawable(Drawable pressedDrawable, Drawable unpressedDrawable) {
StateListDrawable stateListDrawable = new StateListDrawable();
stateListDrawable.addState(new int[]{android.R.attr.state_pressed}, pressedDrawable);
stateListDrawable.addState(StateSet.WILD_CARD, unpressedDrawable);
return stateListDrawable;
}
private Drawable createDrawable(int radius, int color) {
float[] outerRadius = new float[]{radius, radius, radius, radius, radius, radius, radius, radius};
RoundRectShape roundRectShape = new RoundRectShape(outerRadius, null, null);
ShapeDrawable shapeDrawable = new ShapeDrawable(roundRectShape);
shapeDrawable.getPaint().setColor(color);
return shapeDrawable;
}
private void init() {
Resources resources = getResources();
if (resources == null) {
return;
}
this.setTextColor(resources.getColor(R.color.flatbutton_default_text_color));
this.setAllCaps(resources.getBoolean(R.bool.flatbutton_default_textallcaps));
this.setTypeface(Typeface.DEFAULT_BOLD);
this.setTextSize(14f);
this.setHeight(resources.getDimensionPixelSize(R.dimen.flatbutton_default_height));
this.buttonColor = resources.getColor(R.color.colorPrimary);
this.cornerRadius = resources.getDimensionPixelSize(R.dimen.flatbutton_default_corner_radius);
//setPadding();
}
private void parseAttrs(Context context, AttributeSet attrs) {
TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.FlatButton, 0, 0);
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.FlatButton);
if (typedArray == null) return;
for (int i = 0; i < typedArray.getIndexCount(); i++) {
int attr = typedArray.getIndex(i);
if (attr == R.styleable.FlatButton_buttonColor) {
buttonColor = typedArray.getColor(attr, R.color.colorPrimary);
} else if (attr == R.styleable.FlatButton_cornerRadius) {
cornerRadius = typedArray.getDimensionPixelSize(attr, R.dimen.flatbutton_default_corner_radius);
}
}
typedArray.recycle();
int[] attrsArray = new int[]{
android.R.attr.paddingLeft, // 0
android.R.attr.paddingRight, // 1
};
TypedArray ta = context.obtainStyledAttributes(attrs, attrsArray);
if (ta == null) return;
paddingLeft = ta.getDimensionPixelSize(0, 0);
paddingRight = ta.getDimensionPixelSize(1, 0);
ta.recycle();
//Get paddingTop, paddingBottom
int[] attrsArray2 = new int[]{
android.R.attr.paddingTop, // 0
android.R.attr.paddingBottom,// 1
};
TypedArray ta1 = context.obtainStyledAttributes(attrs, attrsArray2);
if (ta1 == null) return;
paddingTop = ta1.getDimensionPixelSize(0, 0);
paddingBottom = ta1.getDimensionPixelSize(1, 0);
ta1.recycle();
}
private void updateBackground(Drawable background) {
if (background == null) return;
//Set button background
if (Build.VERSION.SDK_INT >= 16) {
this.setBackground(background);
} else {
this.setBackgroundDrawable(background);
}
}
public int getButtonColor() {
return buttonColor;
}
public void setButtonColor(int buttonColor) {
this.buttonColor = buttonColor;
}
public int getCornerRadius() {
return cornerRadius;
}
public void setCornerRadius(int cornerRadius) {
this.cornerRadius = cornerRadius;
}
public int getPressedButtonColor() {
return pressedButtonColor;
}
public void setPressedButtonColor(int pressedButtonColor) {
this.pressedButtonColor = pressedButtonColor;
}
public Drawable getPressedDrawable() {
return pressedDrawable;
}
public void setPressedDrawable(Drawable pressedDrawable) {
this.pressedDrawable = pressedDrawable;
}
public Drawable getUnpressedDrawable() {
return unpressedDrawable;
}
public void setUnpressedDrawable(Drawable unpressedDrawable) {
this.unpressedDrawable = unpressedDrawable;
}
}
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="FlatButton">
<attr name="buttonColor" format="color" />
<attr name="cornerRadius" format="dimension" />
</declare-styleable>
</resources>
梦诗
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="flatbutton_default_corner_radius">3dp</dimen>
<dimen name="flatbutton_default_padding_left">10dp</dimen>
<dimen name="flatbutton_default_padding_right">10dp</dimen>
<dimen name="flatbutton_default_padding_top">5dp</dimen>
<dimen name="flatbutton_default_padding_bottom">5dp</dimen>
<dimen name="flatbutton_default_height">30dp</dimen>
<dimen name="flatbutton_default_text_size">14sp</dimen>
</resources>