How to split a screen with two equal LinearLayouts?

Want to split the screen for my application with two LinearLayouts. What parameters should be used for precise separation in two equal parts - first LinearLayout on top, and the second under it.

+55
android android-linearlayout
Aug 6 '10 at 14:50
source share
5 answers

Use the layout_weight attribute. The layout will look something like this:

 <LinearLayout android:orientation="horizontal" android:layout_height="fill_parent" android:layout_width="fill_parent"> <LinearLayout android:layout_weight="1" android:layout_height="fill_parent" android:layout_width="0dp"/> <LinearLayout android:layout_weight="1" android:layout_height="fill_parent" android:layout_width="0dp"/> </LinearLayout> 
+114
Aug 6 2018-10-06T00:
source share

I answer this question after 4-5 years, but it is best done below

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <LinearLayout android:id="@+id/firstLayout" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_toLeftOf="@+id/secondView" android:orientation="vertical"></LinearLayout> <View android:id="@+id/secondView" android:layout_width="0dp" android:layout_height="match_parent" android:layout_centerHorizontal="true" /> <LinearLayout android:id="@+id/thirdLayout" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_toRightOf="@+id/secondView" android:orientation="vertical"></LinearLayout> </RelativeLayout> 

This is the right approach, since using layout_weight is always very difficult for UI operations. Splitting the layout using LinearLayout is not good practice

+41
May 26 '15 at 9:35
source share

Just put it there:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#FF0000" android:weightSum="4" android:padding="5dp"> <!-- to show what the parent is --> <LinearLayout android:background="#0000FF" android:layout_height="0dp" android:layout_width="match_parent" android:layout_weight="2" /> <LinearLayout android:background="#00FF00" android:layout_height="0dp" android:layout_width="match_parent" android:layout_weight="1" /> </LinearLayout> 
+14
Jul 05 '13 at 2:11
source share

To divide the user interface into two equal parts, you can use weightSum of 2 in the parent LinearLayout and assign layout_weight 1 for each, as shown below

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:weightSum="2"> <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:orientation="vertical"> </LinearLayout> <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:orientation="vertical"> </LinearLayout> </LinearLayout> 
+4
Jun 08 '18 at 18:11
source share
 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:orientation="vertical" android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:layout_marginTop="16dp" android:textSize="18sp" android:textStyle="bold" android:padding="4dp" android:textColor="#EA80FC" android:fontFamily="sans-serif-medium" android:text="@string/team_a" android:gravity="center_horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/team_a_score" android:text="@string/_0" android:textSize="56sp" android:padding="4dp" android:gravity="center_horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/team_a_fouls" android:text="@string/fouls" android:padding="4dp" android:textSize="26sp" android:gravity="center_horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:text="@string/_1_points" android:layout_width="match_parent" android:onClick="addOnePointTeamA" android:textColor="#fff" android:layout_margin="6dp" android:layout_height="wrap_content" /> <Button android:text="@string/_2_points" android:textColor="#fff" android:onClick="addTwoPointTeamA" android:layout_width="match_parent" android:layout_margin="6dp" android:layout_height="wrap_content" /> <Button android:text="@string/_3_points" android:textColor="#fff" android:onClick="addThreePointTeamA" android:layout_margin="6dp" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:text="@string/_1_point_foul" android:textColor="#fff" android:layout_width="match_parent" android:onClick="addOnePointFoulTeamA" android:layout_margin="6dp" android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout android:orientation="vertical" android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:text="@string/team_b" android:textColor="#EA80FC" android:textStyle="bold" android:padding="4dp" android:layout_marginTop="16dp" android:fontFamily="sans-serif-medium" android:textSize="18sp" android:gravity="center_horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/team_b_score" android:text="0" android:padding="4dp" android:textSize="56sp" android:gravity="center_horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/team_b_fouls" android:text="Fouls" android:padding="4dp" android:textSize="26sp" android:gravity="center_horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:text="@string/_1_points" android:textColor="#fff" android:fontFamily="sans-serif-medium" android:layout_width="match_parent" android:onClick="addOnePointTeamB" android:layout_margin="6dp" android:layout_height="wrap_content" /> <Button android:text="@string/_2_points" android:layout_margin="6dp" android:fontFamily="sans-serif-medium" android:textColor="#fff" android:onClick="addTwoPointTeamB" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:text="@string/_3_points" android:fontFamily="sans-serif-medium" android:textColor="#fff" android:onClick="addThreePointTeamB" android:layout_margin="6dp" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:text="@string/_1_point_foul" android:textColor="#fff" android:onClick="addOnePointFoulTeamB" android:layout_width="match_parent" android:layout_margin="6dp" android:layout_height="wrap_content" /> </LinearLayout> </LinearLayout> <Button android:text="@string/reset" android:layout_marginBottom="25dp" android:onClick="resetScore" android:textColor="#fff" android:fontFamily="sans-serif-medium" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RelativeLayout> 
-one
Apr 12 '19 at 16:30
source share



All Articles