RelativeLayout layout = new RelativeLayout(this);
RelativeLayout.LayoutParams labelLayoutParams = new RelativeLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
layout.setLayoutParams(labelLayoutParams);
// If you want to add some controls in this Relative Layout
labelLayoutParams = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
labelLayoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
ImageView mImage = new ImageView(this);
mImage.setBackgroundResource(R.drawable.popupnew_bg);
layout.addView(mImage,labelLayoutParams);
setContentView(layout);
5 回答
只是一个基本的例子:
如您所见,这是您必须做的:
创建
RelativeLayout.LayoutParams
对象 .使用
addRule(int)
或addRule(int, int)
设置规则 . 第一种方法用于添加不需要值的规则 .将参数设置为视图(在本例中为每个按钮) .
像这样的东西......
如果您创建它,只需从视图本身拉出布局参数即可 .
我希望下面的代码会有所帮助 . 它将创建一个EditText和一个Log In按钮 . 两者都相对而言 . 全部在MainActivity.java中完成 .