在RelativeLayout中在运行时对齐视图

Sha*_*aaz 3 android relativelayout

如何在运行时在相对布局中添加视图,以使其在父级的顶部对齐.假设我在单击按钮时添加了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);  
Run Code Online (Sandbox Code Playgroud)

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

Ric*_*ich 6

您需要使用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);
Run Code Online (Sandbox Code Playgroud)

addView方法