2
FrameLayout frameLayout = new FrameLayout(this);
LinearLayout linearLayout1= new LinearLayout(this);
LinearLayout linearLayout2= new LinearLayout(this);

FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

params.gravity = Gravity.CENTER_VERTICAL;

frameLayout.addView(linearLayout1, params);

how to add linearLayout2 under linearLayout1

Ala Aga
  • 399
  • 2
  • 4
  • 12
  • What happens when you compile and run this code? – Code-Apprentice Jul 12 '13 at 22:27
  • Can you make the frame layout a Relativelayout and set the layout_below parameter of the new linear layout to be below the the first linear layout ala [here](http://stackoverflow.com/a/3277302/418505) or can you host both linearlayouts in a linearlayout that is oriented vertically? – Selecsosi Jul 12 '13 at 22:28
  • If you need to do something like that, chances that you are doing something wrong are high, definitely there might be another component that is better for this, why dont you better explain what you are trying to accomplish here, so we can help... – Martin Cazares Jul 12 '13 at 22:29
  • If I write frameLayout.addView(linearLayout2, params); at last line , the two layouts set in the same place @MonadNewb – Ala Aga Jul 12 '13 at 22:41

2 Answers2

15

Based on the knowledge that your code works.

Change the FrameLayout for a vertical linear layout i.e. it lays out it's children below each other.

LinearLayout parentLayout = new LinearLayout(this);
LinearLayout linearLayout1= new LinearLayout(this);
LinearLayout linearLayout2= new LinearLayout(this);

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

params.gravity = Gravity.CENTER_VERTICAL;

parentLayout.setOrientation(VERTICAL);
parentLayout.addView(linearLayout1, params);
parentLayout.addView(linearLayout2, params);
Blundell
  • 75,855
  • 30
  • 208
  • 233
1

FrameLayout Documentation states that:

... Child views are drawn in a stack, with the most recently added child on top. The size of the FrameLayout is the size of its largest child (plus padding) ...

So you cannot (at least easily) do what are you want. However a LinearLayout as your root will do this automatically so I suggest to consider using one...

Hope this helps...

ChD Computers
  • 3,135
  • 3
  • 23
  • 33