How to use RelativeLayout to make a custom component on Android

Posted by Styggentorsken on Stack Overflow See other posts from Stack Overflow or by Styggentorsken
Published on 2010-04-19T17:28:15Z Indexed on 2010/04/19 17:33 UTC
Read the original article Hit count: 314

Filed under:
|

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

© Stack Overflow or respective owner

Related posts about android

Related posts about layoutmanager