好的,這是我做的。
private Button drawButton;
public CustomSwitch(Context context) {
super(context);
this.drawButton = new Button(context);
}
public CustomSwitch(Context context, AttributeSet attrs) {
super(context, attrs);
this.drawButton = new Button(context, attrs);
}
public CustomSwitch(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.drawButton = new Button(context, attrs, defStyle);
}
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
this.drawButton.measure(widthMeasureSpec, heightMeasureSpec);
};
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
this.drawButton.layout(left, top, right, bottom);
};
@Override
protected void onDraw(android.graphics.Canvas canvas) {
//...
if(condition) {
this.drawButton.draw(canvas);
}
}
CustomSwitch從視圖擴展? – neworld
CustomSwitch擴展自[Switch](http://developer.android.com/reference/android/widget/Switch.html) –