Splitting RelativeLayout into two halves

Hi, I created a RelativeLayout containing 2 buttons, a switch and a graphical representation.

Now I want to display two different data in a graph.

How to split a RelativeLayout into two halves?

This is my real XML code:

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <Button android:id="@+id/BtnStart" android:layout_width="100dip" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_marginTop="5dip" android:text="SlaveEnable" /> <Button android:id="@+id/BtnStop" android:layout_width="100dip" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_marginTop="5dip" android:layout_toRightOf="@id/BtnStart" android:text="SlaveDisable" /> <RadioButton android:id="@+id/BtnSaveFile" android:layout_width="100dip" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_toRightOf="@id/BtnStop" android:text="SaveFile" /> <helog.diwesh.NugaBest.GraphView android:id="@+id/gview" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@id/BtnStart" android:layout_alignParentRight="true" /> </RelativeLayout> 
+6
source share
4 answers

Try this layout. This should work with some changes if they are not perfect. This will add two graphs.

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <Button android:id="@+id/BtnStart" android:layout_width="100dip" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_marginTop="5dip" android:text="SlaveEnable" /> <Button android:id="@+id/BtnStop" android:layout_width="100dip" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_marginTop="5dip" android:layout_toRightOf="@id/BtnStart" android:text="SlaveDisable" /> <RadioButton android:id="@+id/BtnSaveFile" android:layout_width="100dip" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_toRightOf="@id/BtnStop" android:text="SaveFile" /> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@id/BtnStart" android:layout_alignParentRight="true" android:orientation="vertical" android:weightSum="2" > <helog.diwesh.NugaBest.GraphView android:id="@+id/gview1" android:layout_height="0dp" android:layout_width="match_parent" android:layout_weight="1" /> <helog.diwesh.NugaBest.GraphView android:id="@+id/gview2" android:layout_height="0dp" android:layout_width="match_parent" android:layout_weight="1" /> </LinearLayout> </RelativeLayout> 
+2
source

So, for this, the best way I found (which looks like a general hack, but works like a charm) should have a zero size aligned to the center of the layout and then aligning the left half to the left of this view, align the right half to the right of this view . For instance:

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <View android:id="@+id/anchor" android:layout_width="0dp" android:layout_height="0dp" android:layout_centerInParent="true" /> <Button android:id="@+id/left_side" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_toLeftOf="@id/anchor" android:layout_alignParentBottom="true" android:layout_marginTop="5dip" android:text="Left Side" /> <Button android:id="@+id/right_side" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_toRightOf="@id/anchor" android:layout_alignParentBottom="true" android:layout_marginTop="5dip" android:text="Right Side" /> </RelativeLayout> 
+14
source

Use Linearayout, not RelativeLayout.LinearLayout allows you to split your screen in two vertically or horizontally, as you want, using the layout_weight property of LinearLayout.

With layout_weight you can specify the aspect ratio between several views. For instance. you have a MapView and a table that should display some additional information on the map. The map should use 3/4 of the screen, and the table should use 1/4 of the screen. Then you set the layout_weight of the map to 3 and the layout_weight of the table to 1.

Write your code as shown below, which divides the screen into two parts.

 <LinearLayout android:layout_height="match_parent" android:layout_width="match_parent" android:weightSum="2" android:orientation="vertical"> <LinearLayout android:layout_height="match_parent" android:layout_width="match_parent" android:layout_weight="1"> </LinearLayout> <LinearLayout android:layout_height="match_parent" android:layout_width="match_parent" android:layout_weight="1"> <LinearLayout> 

In the android code : orientation, the orientation is indicated, if you use vertical, then it will massage its child vertically, and if you specify horizontal, it will arrange it horizontally.

+5
source

That should work. I removed RelativeLayout using LinearLayout to use layout_weight .

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight=".50" > <Button android:id="@+id/BtnStart" android:layout_width="100dip" android:layout_height="wrap_content" android:layout_marginTop="5dip" android:text="SlaveEnable" /> <Button android:id="@+id/BtnStop" android:layout_width="100dip" android:layout_height="wrap_content" android:layout_marginTop="5dip" android:text="SlaveDisable" /> <RadioButton android:id="@+id/BtnSaveFile" android:layout_width="100dip" android:layout_height="wrap_content" android:text="SaveFile" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight=".50" > <helog.diwesh.NugaBest.GraphView android:id="@+id/gview" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout> </LinearLayout> 
0
source

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


All Articles