填充所有视图使用LayoutParams.MATCH_PARENT
而不是LayoutParams.WRAP_CONTENT
. 要将布局设置为正方形,只需创建int width,height = 300;
然后:
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(width, height);
或传入LayoutParams.WRAP_CONTENT
并RelativeLayout.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);
最好的祝愿。