如何在Android属性layout_alignParentBottom动态设置到一个按钮按钮、属性、动态、如何在

2023-09-05 09:42:56 作者:跪下叫哥

我有一个动态的布局,我有一个按钮。我怎么可以在酒店 layout_alignParentBottom 设置为按钮,它会出现在我的相对布局的底部?

I have a dynamic layout and I have a button. How can I set the property layout_alignParentBottom to that button so that it would appear in the bottom of my relative layout?

或者你知道另一种方式?

Or do you know another way?

推荐答案

这只会工作,如果你的布局是一个RelativeLayout的,因为其他ViewGroups不明白alignParentBottom。

This will only work if your layout is a RelativeLayout, as other ViewGroups don't understand 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);