我正在尝试为复选框设置自定义背景,但它加倍了,我不知道为什么。
我的主要活动:
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
CheckBox cb = new CheckBox(getApplicationContext());
DrawableCheckButton d = new DrawableCheckButton(getApplicationContext());
cb.setBackgroundDrawable(d);
setContentView(cb);
}
DrawableCheckButton 类:
public class DrawableCheckButton extends StateListDrawable {
public DrawableCheckButton(Context context) {
int[] state_checked = { android.R.attr.state_checked };
int[] state_active = { android.R.attr.state_enabled };
CustomDrawableCheckButton d = new CustomDrawableCheckButton(context);
CustomDrawableCheckButton2 d2 = new CustomDrawableCheckButton2(context);
addState(state_checked, d);
addState(state_active, d2);
}
}
另一个班级看起来像这样
public class CustomDrawableCheckButton extends Drawable{
private Context context;
public CustomDrawableCheckButton(Context context) {
// TODO Auto-generated constructor stub
this.context = context;
}
@Override
public void draw(Canvas canvas) {
Path path1 = new Path();
Paint paint1 = new Paint();
path1.moveTo(0, 3);
path1.lineTo(17, 3);
path1.moveTo(0, 3);
path1.lineTo(0, 23);
path1.lineTo(20, 23);
path1.lineTo(20, 14);
paint1.setColor(context.getResources().getColor(R.color.orange));
paint1.setStrokeWidth(1);
paint1.setStyle(Paint.Style.STROKE);
//
Path path2 = new Path();
Paint paint2 = new Paint();
path2.moveTo(4,10);
path2.lineTo(10, 16);
path2.lineTo(22, 4);
paint2.setColor(context.getResources().getColor(R.color.purple));
paint2.setStrokeWidth(5);
paint2.setStyle(Paint.Style.STROKE);
canvas.drawPath(path1, paint1);
canvas.drawPath(path2, paint2);
}
我需要帮助。