Android 如何为动态创建的按钮设置参数?

Android 如何为动态创建的按钮设置参数?,android,button,Android,Button,我已经动态创建了按钮现在我想为动态创建的按钮设置参数,例如(layout_marginTop或layout_marginRight),我如何使用RelativeLayout layout来实现这一点 谢谢您好,您可以使用以下代码进行此操作 RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.Layout

我已经动态创建了按钮现在我想为动态创建的按钮设置参数,例如(layout_marginTop或layout_marginRight),我如何使用RelativeLayout layout来实现这一点


谢谢

您好,您可以使用以下代码进行此操作

  RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.FILL_PARENT);
            lp.setMargins(arg0, arg1, arg2, arg3);

button.setLayoutParams(lp);

您需要更改按钮的
布局参数

View button = /* ... */;
RelativeLayout.LayoutParams p =
    new RelativeLayout.LayoutParams(button.getLayoutParams());
p.topMargin = /* ... */;
p.rightMargin = /* ... */;
// or use p.setMargins() method.
button.setLayoutParams(p);