开发者

Aligning views at runtime in RelativeLayout

开发者 https://www.devze.com 2023-02-12 08:09 出处:网络
How do I add a view in relative layout at runtime, so that it aligns itself at the Top of the parent. Say I am adding an EditText on clicking the button. Now this EditText should come in such a way th

How do I add a view in relative layout at runtime, so that it aligns itself at the Top of the parent. Say I am adding an EditText on clicking the button. Now this EditText should come in such a way that EditText comes 开发者_运维知识库above the Button.

here is an example of what I am doing.

    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);  

I am not able to make the alignment at all. Any clue is highly appreciated. Thanks in Advance.


You need to use the overload of RelativeLayout.addView that takes your LayoutParams as a second parameter

    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


Dont go such complex

Instead

Step 1 : Add a Linear Layout with Vertical orientation where so ever you are adding your button

Step 2 : First Add EditText and Set its Visibility to GONE

Step 3 : Add Your Button

Step 4 : On button click simply make your button VISIBLE

This will work for sure and is easy to implement to

Hope it helps :)

0

精彩评论

暂无评论...
验证码 换一张
取 消