6

У меня возникают проблемы с выравниванием фрагментов с помощью RelativeLayout, хотя кажется, что это должно быть просто. Мне просто нужно два фрагмента рядом друг с другом, и если я использую LinearLayout он работает отлично:Выравнивание фрагментов с RelativeLayout

<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

Однако, если я использую RelativeLayout, ничего не показывает:

<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

Обновление:

Вот скриншот того, что я вижу:

enter image description here

Это код, я использую:

<?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> 

ответ

5

Вы не можете использовать веса с RelativeLayout. В основном этот атрибут игнорируется, что означает, что оба фрагмента визуализируются с шириной 0, поэтому они не видны.

Я не уверен, что вы пытаетесь выполнить, но вы можете рассмотреть вопрос об упаковке первого примера (LinearLayout) в RelativeLayout - другими словами: объединить/объединить оба макета. Тем не менее, это приводит к еще одному уровню в иерархии вашего представления.


Edit:

Я на самом деле не проверял, но я думаю, что-то вроде этого является то, что вы ищете:

<?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> 

Очевидно, что вы можете заменить TextView с чем угодно.

+0

То, что я в конечном счете пытаюсь сделать, это добавить нижний колонтитул к нижней части макета, но я не мог заставить его работать с помощью 'LinearLayout'. Мне удалось отобразить нижний колонтитул с помощью «RelativeLayout», но тогда фрагменты не появлялись, поэтому я просто пытался получить фрагменты (без нижнего колонтитула), следовательно, мой вопрос. Похоже, мне нужно будет выяснить, как заставить нижний колонтитул работать с помощью «LinearLayout». Я в основном хочу макет, где левый фрагмент занимает 1/3 экрана, справа - остальная часть экрана, с липким нижним колонтитулом. –

+0

Правильно, это не должно быть слишком сложно реализовать. См. Редактирование в моем ответе и дайте это. Не пробовал, но должен быть (близко к), что вам нужно. –

+0

Это отлично поработало. Благодаря! –

Смежные вопросы