我這樣定義一個類:自己定義的佈局,的onDraw()方法沒有得到所謂的
public class TestMyFrameLayout extends FrameLayout{
Paint mPaint;
public TestMyFrameLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public TestMyFrameLayout(Context context) {
super(context);
mPaint = new Paint();
mPaint.setColor(Color.GREEN);
}
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawCircle(50f, 50f, 30, mPaint);
}
}
,並要求它像:
TestMyFrameLayout myFrameLayout = new TestMyFrameLayout(this);
LayoutParams myFrameLayoutParams = new LayoutParams(300,300);
myFrameLayout.setLayoutParams(myFrameLayoutParams);
setContentView(myFrameLayout);
但實際上TestMyFrameLayout.onDraw(帆布油畫)函數沒有被調用,爲什麼?
您可以顯示代碼在哪裏使用它嗎?在onDraw之前還要添加'@ Override':這將檢查日食中的拼寫錯誤。 –
謝謝Laurent'。我的問題已經解決了。添加this.setWillNotDraw(false); –