2013-06-25 76 views
0

我的RelativeLayout在layout/activity.xml中的RelativeLayout廣場

創建創建細胞,我想通過以下方式編程方式添加一些元素有:

RelativeLayout rlayout = (RelativeLayout) findViewById(R.id.relativeLayout1); 
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
rlayout.addView(CustomView,p); 

和它的作品,但被添加的元素沒有按」填滿所有視圖,但我需要它。

並且我還想在square(width = height)中添加這樣的元素,我該怎麼做呢?

回答

0

填寫全部查看使用LayoutParams.MATCH_PARENT而不是LayoutParams.WRAP_CONTENT。 爲了使佈局廣場僅創建int width,height = 300;然後:

RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(width, height); 

或通過LayoutParams.WRAP_CONTENTRelativeLayout.LayoutParams並更改自定義視圖的高度和寬度。

使視圖方:

Button customView = new Button(this); 
    customView.setLayoutParams(new RelativeLayout.LayoutParams(200, 200)); 

    RelativeLayout rlayout = (RelativeLayout) findViewById(R.id.relativeLayout1); 
    rlayout.addView(customView); 

作出填補所有查看您可以使用此:

Button customView = new Button(this); 
    customView.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); 

    RelativeLayout rlayout = (RelativeLayout) findViewById(R.id.relativeLayout1); 
    rlayout.addView(customView); 

你也可以爲你的佈局類似的商品添加規則:

 p.addRule(RelativeLayout.ALIGN_PARENT_LEFT); 

祝好。