如何在Android中动态设置按钮的layout_alignParentBottom属性

12
我有一个动态布局和一个按钮,如何将属性layout_alignParentBottom设置到该按钮上,使它出现在相对布局的底部?或者您知道其他方法吗?
1个回答

57

只有在您的布局是RelativeLayout时,此方法才能生效,因为其他ViewGroups无法理解alignParentBottom。

RelativeLayout layout = findViewById(R.id.my_relative_layout);
Button button = new Button(this); // your button
lp = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); // You might want to tweak these to WRAP_CONTENT
lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
layout.addView(button, lp);

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