自定义的 Layout 方法没有被调用

我定义了一个类:

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 (Canvas 画布)函数没有被调用,为什么?

23417 次浏览

Solved. Add this.setWillNotDraw(false); in constructor