当前位置 : 主页 > 手机开发 > android >

android – 在RelativeLayout中在运行时对齐视图

来源:互联网 收集:自由互联 发布时间:2021-06-11
如何在运行时在相对布局中添加视图,以使其在父级的顶部对齐.假设我在单击按钮时添加了EditText.现在,这个EditText应该以EditText位于Button之上. 这是我正在做的一个例子. button = new Button
如何在运行时在相对布局中添加视图,以使其在父级的顶部对齐.假设我在单击按钮时添加了EditText.现在,这个EditText应该以EditText位于Button之上.

这是我正在做的一个例子.

button = new Button(this);  //here I am just checking if the button can come above the EditText
    button.setText("I am a button");  
    params.addRule(RelativeLayout.BELOW, editText.getId());  
    params.addRule(RelativeLayout.ALIGN_PARENT_ABOVE);  
    relativeLayout.addView(button);

我根本无法进行对齐.
任何线索都非常感谢.
提前致谢.

您需要使用RelativeLayout.addView的重载,它将LayoutParams作为第二个参数

button = new Button(this);  //here I am just checking if the button can come above the EditText
    button.setText("I am a button");  
    params.addRule(RelativeLayout.BELOW, editText.getId());  
    params.addRule(RelativeLayout.ALIGN_PARENT_ABOVE);  
    relativeLayout.addView(button, params);

addView method

网友评论