我有一个自定义按钮,并通过编程方式更改其按下和默认颜色.
public class CustomApplicationButton extends Button {
public CustomApplicationButton(Context context) {
this(context, 0, 0, 0);
}
public CustomApplicationButton(Context context, int topDrawableResId, int outlineDefaultColorId, int outlinePressedColorId) {
super(context);
// set width and height
LinearLayout.LayoutParams params = new LayoutParams(
context.getResources().getDimensionPixelSize(R.dimen.sr_application_button_width),
context.getResources().getDimensionPixelSize(R.dimen.sr_application_button_height));
setLayoutParams(params);
// set drawable top icon
if (topDrawableResId != 0) {
setCompoundDrawablesWithIntrinsicBounds(0, topDrawableResId, 0, 0);
}
// set background and outline color
int strokeWidth = context.getResources().getDimensionPixelSize(R.dimen.sr_launcher_button_stroke_size);
// unpressed state drawable
LayerDrawable defaultLayers = (LayerDrawable) context.getResources().getDrawable(
R.drawable.btn_launcher_shape_default);
GradientDrawable defaultShapeOutline = (GradientDrawable) defaultLayers.findDrawableByLayerId(R.id.outline_default); …Run Code Online (Sandbox Code Playgroud)