自定义布局,onDraw()方法不被调用

时间:2011-10-16 02:06:43

标签: android android-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 canvas)函数没有被调用,为什么?

1 个答案:

答案 0 :(得分:192)

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