自己定义的Layout,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(Canvas canvas)函数没有被调用,为什么呢?

解决了。 添加this.setWillNotDraw(false); 在构造函数中