How to add LinearLayout under another LinearLayout in a FrameLayout programmatically?
12,289
Solution 1
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);
Solution 2
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...
Author by
Ala Aga
Updated on June 05, 2022Comments
-
Ala Aga almost 2 years
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
-
Code-Apprentice almost 11 yearsWhat happens when you compile and run this code?
-
Selecsosi almost 11 yearsCan 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 or can you host both linearlayouts in a linearlayout that is oriented vertically?
-
Martin Cazares almost 11 yearsIf 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...
-
Ala Aga almost 11 yearsIf I write frameLayout.addView(linearLayout2, params); at last line , the two layouts set in the same place @MonadNewb
-
-
Ala Aga almost 11 yearsan error occurs at (VERTICAL) in setOrientation method @Blundell
-
Ala Aga almost 11 yearsI solved It , you should write LinearLayout.VERTICAL , thanks