Android, how to place two relative layouts, one to the left and one to the right of the screen?

In my Android application (for orientation in landscape orientation) I need to place widgets on two relative layouts, one to the left of the screen and one to the right (to fill the full size).

I prefer to work programmatically (I find it more flexible than xml).

Shoud Should I use TableLayout as the parent layout for my sub-layouts?

+2
source share
2 answers

For just two RelativeLayouts next to each other, you have the choice to archive. Horizontal LinearLayout would be the easiest in my opinion.


Edit: I never make layouts in code, but since you are probably reading a lot of XML documents, you should translate this example. Uses 50/50 space allocation for both layouts.

 <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal"> <RelativeLayout android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="1" > </RelativeLayout> <RelativeLayout android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="1" > </RelativeLayout> </LinearLayout> 

Edit 2:

Definitely works, just tried this:

 LinearLayout layoutContainer = new LinearLayout(this); layoutContainer.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); // Arguments here: width, height, weight LinearLayout.LayoutParams childLp = new LinearLayout.LayoutParams(0, LayoutParams.FILL_PARENT, 1); RelativeLayout layoutLeft = new RelativeLayout(this); layoutContainer.addView(layoutLeft, childLp); RelativeLayout layoutRight = new RelativeLayout(this); layoutContainer.addView(layoutRight, childLp); 
+10
source

Answering my own question:

The method suggested by alextsc does not work, since RelativeLayouts (unlike LinearLayouts) have no weight.

I decided with this (ugly :-() hack:

 LinearLayout layoutContainer = new LinearLayout(myActivity.this); layoutContainer.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); int width = getWindowManager().getDefaultDisplay().getWidth() / 2; RelativeLayout layoutLeft = new RelativeLayout(Results.this); layoutContainer.addView(layoutLeft, width, LayoutParams.FILL_PARENT); RelativeLayout layoutRight = new RelativeLayout(Results.this); layoutContainer.addView(layoutRight, width, LayoutParams.FILL_PARENT); 
0
source

Source: https://habr.com/ru/post/897088/


All Articles