开发者

How do you set the size and position of a ViewGroup that is inside a RelativeLayout? (All Java, no XML)

开发者 https://www.devze.com 2023-03-01 16:31 出处:网络
I\'ve been trying to understand how to draw graphics on Android using a hierarchy of custom views, custom viewgroups, and relativeLayouts. I\'m having a hard time figuring out how to have the parents

I've been trying to understand how to draw graphics on Android using a hierarchy of custom views, custom viewgroups, and relativeLayouts. I'm having a hard time figuring out how to have the parents tell the children where to position themselves and at what size. I think it is working when the custom ViewGroup tells its children to be a certain size, but I can't figure out how to have the relativeLayout tell its ViewGroup children what size and position to be. Right now, the following code draws a circle in the top and center of the screen. (This is Android 3.0 on the Xoom). The size of the circle is 295x295, so I'm assuming this is somehow the default size and position of a layout? Can anyone give me the code that would be needed so that I could programatically pick the size and location of the viewgroup that contains the view that contains the circle? The edges of the circle on the four sides are getting trimmed off just so, so I know that the relativelayout is cutting off the rest of the viewgroup from printing. If I have the ViewGroup measure() the View at a smaller size, it will print it at that size up in the left corner of that same rectangle in the top center of the screen.

My Activity's onCreate:

 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    CustomViewGroup custVG = new CustomViewGroup(this);
    RelativeLayout rel = new RelativeLayout(getApplicationContext());
    rel.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

    rel.addView(custVG);

    RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,  
                    LayoutParams.WRAP_CONTENT);  
    params2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);  

    rel.addView(new CustomViewGroup(getApplicationContext()),1,
            new ViewGroup.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));

    setContentView(rel);

}

My Custom made ViewGroup:

CustomView cust;
int _height;
int _width;

public CustomViewGroup(Context context) {
    super(context);
    cust = new CustomView(context);

    Canvas canvas = new Canvas();

    this.addView(cust,new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); 
}

@Override 
public void onDraw(Canvas canvas){
    this.drawChild(canvas,cust,0);
}




@Override
protected void onLayout(boolean arg0, int arg1, int arg2, int arg3, int arg4) {

    cust.layout(this.getLeft(), this.getTop(), this.getRight(), this.getBottom());
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){

    //Measure the only child
    this.measureChild(cust,widthMeasureSpec, heightMeasureSpec);

    _he开发者_如何学JAVAight = View.MeasureSpec.getSize(heightMeasureSpec);
    _width = View.MeasureSpec.getSize(widthMeasureSpec);
    setMeasuredDimension(_height, _width);
}

My Custom View

public CustomView(Context context){
    super(context);

}

@Override
public void onDraw(Canvas canvas) {

    float noteRadius = _width/2;

    Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setColor(Color.YELLOW);
    paint.setStyle(Paint.Style.STROKE);
    float strokeWidth = paint.getStrokeWidth();
    paint.setStrokeWidth(strokeWidth);

    canvas.drawCircle(_height/2, _width/2, noteRadius, paint);

}

   @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        _height = View.MeasureSpec.getSize(heightMeasureSpec);
        _width = View.MeasureSpec.getSize(widthMeasureSpec);
        Log.i("CustomView onMeasure","Width: " + _width + " Height: " + _height);

        setMeasuredDimension(_height, _width);
    }

Sorry this is long. Thanks!


Okay - I figured out the problem. 1) I was missing the <uses-sdk android:minSdkVersion="11" /> line in my Manifest so it wasn't filling the screen of the Xoom.

2) I didn't realize that you don't have to just use MATCH_PARENT or WRAP_CONTENT for the layout params. For instance the following will create a circle of size 500x500 and put it in the bottom left of the screen (it goes off the screen as expected).

RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(500,  
                    500);  
    params2.leftMargin = 0;
    params2.topMargin = 200;
rel.addView(new CustomViewGroup(this),1,params2);
0

精彩评论

暂无评论...
验证码 换一张
取 消