views:

266

answers:

0

I'm trying to make a custom component based on buttons without having to deal with all the layout myself. The problem is that i can't find a way to make use of RelativeLayout to draw the thing. This is my latest attempt. A bitmap is returned by layoutManager.getDrawingCache(), but it does not show any buttons. canvas.drawColor(...) however works.

public class MyView extends View {

    Button myButton;
    RelativeLayout layoutManager;

    public MyView(Context context) {
        super(context);
        initMyView();
    }

    public MyView(Context context, AttributeSet attrs) {
        super(context);
        initMyView();
        // ...
    }

    public void initMyView() {
        layoutManager = new RelativeLayout(getContext());
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(
            MeasureSpec.getSize(measureSpec),
            MeasureSpec.getSize(measureSpec) );
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        myButton = new Button(layoutManager.getContext());
        myButton.setVisibility(VISIBLE);
        myButton.setDrawingCacheEnabled(true);
        layoutManager.addView(myButton);

        layoutManager.setDrawingCacheEnabled(true);
        layoutManager.layout(0, 0, 150, 150);
        layoutManager.setVisibility(VISIBLE);
        layoutManager.buildDrawingCache();
        layoutManager.draw(canvas);
        canvas.drawBitmap(layoutManager.getDrawingCache(), 0.0f, 0.0f, null);
    }
}