【问题标题】:How do you set the size and position of a ViewGroup that is inside a RelativeLayout? (All Java, no XML)如何设置相对布局内的 ViewGroup 的大小和位置? (所有 Java,没有 XML)
【发布时间】:2011-05-29 15:38:05
【问题描述】:

我一直试图了解如何在 Android 上使用自定义视图、自定义视图组和 relativeLayouts 的层次结构绘制图形。我很难弄清楚如何让父母告诉孩子们自己的位置和尺寸。我认为当自定义 ViewGroup 告诉其子项具有特定大小时它正在工作,但我无法弄清楚如何让 relativeLayout 告诉其 ViewGroup 子项的大小和位置。现在,以下代码在屏幕的顶部和中心绘制了一个圆圈。 (这是 Xoom 上的 Android 3.0)。圆圈的大小是 295x295,所以我假设这是布局的默认大小和位置?谁能给我需要的代码,以便我可以以编程方式选择包含包含圆的视图的视图组的大小和位置?四个边上的圆的边缘被修剪掉了,所以我知道相对布局正在从打印中切断视图组的其余部分。如果我有 ViewGroup measure() 较小尺寸的视图,它将在屏幕顶部中心的同一矩形的左角以该尺寸向上打印。

我的 Activity 的 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);

}

我的自定义视图组:

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

    _height = View.MeasureSpec.getSize(heightMeasureSpec);
    _width = View.MeasureSpec.getSize(widthMeasureSpec);
    setMeasuredDimension(_height, _width);
}

我的自定义视图

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

对不起,这很长。谢谢!

【问题讨论】:

    标签: android android-layout android-3.0-honeycomb


    【解决方案1】:

    好的 - 我找到了问题所在。 1) 我的 Manifest 中缺少 <uses-sdk android:minSdkVersion="11" /> 行,所以它没有填满 Xoom 的屏幕。

    2) 我没有意识到您不必只使用 MATCH_PARENT 或 WRAP_CONTENT 作为布局参数。例如,下面将创建一个大小为 500x500 的圆圈并将其放在屏幕的左下方(它会按预期离开屏幕)。

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

    【讨论】:

      猜你喜欢
      • 2011-08-06
      • 1970-01-01
      • 1970-01-01
      • 2018-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多