Android: How to split a LinearLayout in two parts of exactly the same size?

I have the following layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#FFFFFF" android:orientation="horizontal" android:weightSum="2"> <fragment xmlns:android="http://schemas.android.com/apk/res/android" android:name="ch.lexs.view.LawList" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1"> </fragment> <fragment xmlns:android="http://schemas.android.com/apk/res/android" android:name="ch.lexs.view.ParagraphList" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1"> </fragment> </LinearLayout> 

My intention was to split the LinearLayout into two parts that are exactly the same size. I thought this could be done using weight. But the second fragment is smaller. What am I doing wrong?

+6
source share
6 answers

Try setting the width of both fields to "fill_parent".

+7
source

this code can help you

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" tools:context=".MainActivity" > <fragment android:id="@+id/mainFragment" android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" class="com.lmhuan.weatherapp.helper.MainFragment" > </fragment> <fragment android:id="@+id/detailFragment" android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" class="com.lmhuan.weatherapp.helper.DetailFragment" > </fragment> </LinearLayout> 
+8
source

Do you have layout_height both fragments set to "fill_parent" Perhaps try without it?

+2
source

Try setting android: layout_height = "0" if you want the layouts to have the same height or android: layout_width = "0" if you want to have the same width.

0
source

You must refer to the weightSum attribute of the linear layout. Then the attributes layout_weight , layout_height and layout_width internal views. This is all you need to divide the layout into any ratio.

If you want to split the layout into two parts of the same size, you must set the layout_width attributes to 0dp both layouts and layout_weight to 1 inside the main layout. Then set weightSum to 2 parent layout.

The same concept can be applied to divide the layout into any ratio. Set all internal layout_weight values layout_weight sum of weightSum . Then set layout_width or layout_height to 0dp according to the orientation you want to split. Here is a mock solution for this question.

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" tools:context=".MainActivity" android:weightSum="2"> <fragment android:id="@+id/mainFragment" android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent"> </fragment> <fragment android:id="@+id/detailFragment" android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent"> </fragment> </LinearLayout> 

here is an example to divide the linear alignment by 1, 3, 2 vertically.

 <?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="vertical" android:weightSum="6"> <RelativeLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:background="@android:color/white"> </RelativeLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="3" android:background="@android:color/black"> </RelativeLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="2" android:background="@android:color/white"> </RelativeLayout> </LinearLayout> 
0
source

fill_parent is deprecated so you can use match_parent

0
source

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


All Articles