简体   繁体   中英

How to align in RelativeLayout properly?

Hi I am trying to design a layout programatically where I have a textview and a edittext and a button underneath. Two rows. First row with TextView. Second row with Edittext and button. Edittext should extend from left to the button. Right now, the Textview appears in the first row, but the second row (with Edittext and Button) is messed. Right now, the EditText doesnt extend all the way to the send button. I tried using Wrap_Content, Fill_parent, Match_Parent but no luck. Here is what I have so far:

   RelativeLayout  layout = new RelativeLayout(this);
   EditText myText = new EditText(this);
   Button myBtn = new Button(this);
   parameters_layout = new
             RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 
                    RelativeLayout.LayoutParams.WRAP_CONTENT);
   parameters_layout.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
   parameters_layout.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
   myText.setId(1);
   myText.setLayoutParams(parameters_layout);
   layout.addView(myText,parameters_layout);

   parameters_layout =  new 
               RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 
               RelativeLayout.LayoutParams.WRAP_CONTENT);
   parameters_layout.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);    
   parameters_layout.addRule(RelativeLayout.RIGHT_OF, myText.getId());
   myBtn.setLayoutParams(parameters_layout);
   layout.addView(myBtn,parameters_layout);

Thank you any help is appreciated.

EDIT: Clarification I know

          parameters_layout =  new 
           RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 
           RelativeLayout.LayoutParams.WRAP_CONTENT);

can be replaced with numbers. For example:

          parameters_layout =  new 
           RelativeLayout.LayoutParams(350, 
           RelativeLayout.LayoutParams.WRAP_CONTENT);

Is that 350 just pixels, dp, sp? What is it?

What I understand is what you want to implement is one Button on the bottom-right corner of the screen and one extended EditText just left of the button. Am I correct?

If that, try this. I tested this code in on my galaxy nexus and it worked fine.

RelativeLayout layout = new RelativeLayout(this);
EditText myText = new EditText(this);
Button myBtn = new Button(this);
myBtn.setId(1);

RelativeLayout.LayoutParams parameters_layout = new
    RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT);
parameters_layout.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
parameters_layout.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
parameters_layout.addRule(RelativeLayout.LEFT_OF, myBtn.getId());

myText.setLayoutParams(parameters_layout);
layout.addView(myText,parameters_layout);

parameters_layout =  new   
        RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
        RelativeLayout.LayoutParams.WRAP_CONTENT);
parameters_layout.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
parameters_layout.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
myBtn.setLayoutParams(parameters_layout);

layout.addView(myBtn,parameters_layout);

One tip is when you use RelativeLayout, first locate the view that the position is fixed (in your case bottom-right cornered button) then locate other views by using relative layout specific parameters. (In your case LEFT_OF).

Another tip is whenever if you need to assign a ID value in programmatically, don't assign any specific value (in your case '1'), instead of it you can define ID as a resource, and then use it w/o any worries about id conflict or some strange behaviors.

There is a document about ID resource, so check it - http://developer.android.com/guide/topics/resources/more-resources.html#Id

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM