Fragment alignment with RelativeLayout

I'm having real issues aligning fragments with RelativeLayout , although it seems like it should be simple. I just need two fragments next to each other, and if I use LinearLayout , it works fine:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent"> <fragment android:name="com.fragment.test.TitlesFragment" android:id="@+id/fragmentTitles" android:layout_weight="1" android:layout_width="0dp" android:layout_height="fill_parent" /> <FrameLayout android:id="@+id/details" android:layout_weight="1" android:layout_width="0px" android:layout_height="fill_parent" /> </LinearLayout> 

enter image description here

However, if I use RelativeLayout , it shows nothing:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent"> <fragment android:name="com.fragment.test.TitlesFragment" android:id="@+id/fragmentTitles" android:layout_weight="1" android:layout_width="0dp" android:layout_height="fill_parent" /> <FrameLayout android:id="@+id/details" android:layout_weight="1" android:layout_width="0px" android:layout_height="fill_parent" android:layout_toRightOf="@id/fragmentTitles" /> </RelativeLayout> 

enter image description here

Update:

Here is a screenshot of what I see:

enter image description here

This is the code I'm using:

 <?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" > <LinearLayout android:orientation="horizontal" android:layout_height="fill_parent" android:layout_width="fill_parent" android:weightSum="3" > <fragment android:name="com.fragment1" android:id="@+id/fragment1" android:layout_weight="1" android:layout_width="0dp" android:layout_height="fill_parent" android:layout_above="@id/statusUpdated" /> <fragment android:name="com.fragment2" android:id="@+id/fragment2" android:layout_width="0px" android:layout_weight="2" android:layout_height="fill_parent" android:layout_above="@id/statusUpdated" android:layout_toRightOf="@id/fragment1" /> </LinearLayout> <TextView android:id="@+id/statusUpdated" style="@style/Status" /> </RelativeLayout> 
+6
source share
1 answer

You cannot use weight with RelativeLayout . Basically this attribute is ignored, which means that both fragments are rendered with a width of 0 , so they are not visible.

I'm not sure what you are trying to accomplish, but you might consider moving the first example ( LinearLayout ) to RelativeLayout - in other words: merge / merge both layouts. This leads to another level in the hierarchy of your presentation.


Edit:

I have not actually tested it, but I believe that this is what you are looking for:

 <?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"> <LinearLayout android:id="@+id/fragment_layout" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_above="@+id/footer_view" android:weightSum="3"> <fragment android:id="@+id/fragmentTitles" android:name="com.fragment.test.TitlesFragment" android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="1" /> <FrameLayout android:id="@+id/details" android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="2" /> </LinearLayout> <TextView android:id="@+id/footer_view" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:background="#f00" android:text="I am a footer" /> </RelativeLayout> 

Obviously, you can replace TextView with anything.

+5
source

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


All Articles