Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Own defined Layout , onDraw() method not getting called

I defined a class like that:

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);
    }   
}

and called it like:

TestMyFrameLayout myFrameLayout = new TestMyFrameLayout(this);
LayoutParams myFrameLayoutParams = new LayoutParams(300,300);
myFrameLayout.setLayoutParams(myFrameLayoutParams);
setContentView(myFrameLayout);

But In fact TestMyFrameLayout.onDraw(Canvas canvas) function not getting called, why?

like image 503
old_hou Avatar asked Oct 16 '11 02:10

old_hou


1 Answers

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

like image 182
old_hou Avatar answered Nov 13 '22 22:11

old_hou