Configure settings in child RelativeLayout views

I'm having trouble finding exactly the syntax I need to use to set the parameters in the child views of the relative layout. I have a root relative layout in which I want to set 2 child text images next to each other, e.g.

  ---------- ---------
 |  Second |  |  First |
 ---------- ---------

So, I have a

public class RL extends RelativeLayout{ public RL(context){ TextView first = new TextView(this); TextView second = new TextView(this); first.setText('First'); first.setId(1); second.setText('Second'); second.setId(2); addView(first, new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, LayoutParams.ALLIGN_PARENT_RIGHT ???); addView(first, new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, LayoutParams.ALLIGN_RIGHT_OF(first.getId()) ???); } } 

How to set relative alignments?

+4
source share
2 answers
 public class RL extends RelativeLayout { public RL(Context context) { super(context); TextView first = new TextView(context); TextView second = new TextView(context); first.setText("First"); first.setId(1); second.setText("Second"); second.setId(2); RelativeLayout.LayoutParams lpSecond = new RelativeLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); addView(second, lpSecond); RelativeLayout.LayoutParams lpFirst = new RelativeLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); lpFirst.addRule(RelativeLayout.RIGHT_OF, second.getId()); addView(first, lpFirst); } } 

You only need ALIGN_PARENT_RIGHT if you want the right edge of the view to match the right edge of its parent. In this case, he will put forward the "first" view from the side of the visible region!

+14
source

Falmarri, you need to use the addRule (int) method.

 RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams (LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); lp.addRule(RIGHT_OF, first.getId()); 

A complete list of constants that can be used for addRule can be found here: http://developer.android.com/reference/android/widget/RelativeLayout.html

And here is the addRule method link: http://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html#addRule(int,%20int )

+3
source

All Articles