如何通过代码设置Android视图的layout_alignParentEnd属性?

8

我如何编写程序来设置alignParentEnd属性?

我在整个Android开发者网站上搜索,但是我无法找到有关在代码中设置alignParentEnd等属性的任何参考信息。只有在XML中如何设置它们的说明。将来我在哪里可以找到类似这样的文档?

2个回答

8

LayoutParams 用于在代码中设置布局属性。 alignParentEndRelativeLayout 的属性,因此您必须使用 RelativeLayout.LayoutParams。 您可以在这里找到文档。


因此,要在代码中设置属性,您可以使用RelativeLayout.LayoutParamsaddRule()方法。例如,您可以执行以下操作:

// Create the LayoutParams
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

// Add all the rules you need
params.addRule(RelativeLayout.ALIGN_PARENT_END);
...

// Once you are done set the LayoutParams to the layout
relativeLayout.setLayoutParams(params);

你可以在这里找到所有可以用addRule()设置的规则列表(链接)

params.addRule(RelativeLayout.ALIGN_PARENT_END); Field requires API level 17 android.widget.RelativeLayout#ALIGN_PARENT_END - John

5

使用LayoutParam在View和ViewGroup中添加参数。

RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) my_child_view.getLayoutParams();
params.addRule(RelativeLayout.ALIGN_PARENT_END);
my_child_view.setLayoutParams(params);

这下可行了!!! - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接