Answers:
是:
RelativeLayout.LayoutParams params= new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.BELOW, R.id.below_id);
viewToLayout.setLayoutParams(params);
首先,代码通过指定高度和宽度来创建新的布局参数。该addRule
方法正确添加了xml的等效项android:layout_below
。然后,您只需调用View#setLayoutParams
要具有这些参数的视图。
或者,您可以使用视图的当前布局参数并对其进行修改:
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) viewToLayout.getLayoutParams();
params.addRule(RelativeLayout.BELOW, R.id.below_id);
LayoutParams
而不是创建新视图。实际上,这应该被接受为正确答案。
虽然@jackofallcode答案是正确的,但可以写成一行:
((RelativeLayout.LayoutParams) viewToLayout.getLayoutParams()).addRule(RelativeLayout.BELOW, R.id.below_id);
LayoutParams
来设置setLayoutParams