3

Я пытаюсь создать простой макет с тремя фрагментами. Слева, я хочу два фрагмента над каждым, каждый из которых занимает 50% высоты экрана. Справа, я хочу один большой фрагмент контейнера, например:Использование RelativeLayout для вертикального разделения двух фрагментов равномерно

+-----+-----------------+ 
| f1 | detail_container| 
|  |     | 
+-----+     | 
| f2 |     | 
|  |     | 
+-----+-----------------+ 

Я получил это работает с двумя LinearLayouts, используя layout_height="0dp" и layout_weight="1", но я получил сообщение, что это плохо сказывается на производительности, поэтому я решил используйте RelativeLayout. То, что я сейчас это:

<LinearLayout 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" 
    android:layout_marginLeft="0dp" 
    android:layout_marginRight="0dp" 
    android:baselineAligned="false" 
    android:divider="?android:attr/dividerHorizontal" 
    android:orientation="horizontal" 
    android:showDividers="middle" 
    tools:context=".MainActivity" > 
    <RelativeLayout 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:orientation="vertical" 
     android:layout_weight="1" > 

     <fragment 
      android:id="@+id/fragment1" 
      android:name="com.example.Fragment1" 
      android:layout_width="wrap_content" 
      android:layout_height="" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentTop="true" 
      tools:layout="@android:layout/list_content" /> 

     <fragment 
      android:id="@+id/fragment2" 
      android:name="com.example.Fragment2" 
      android:layout_width="wrap_content" 
      android:layout_height="" 
      android:layout_alignParentBottom="true" 
      android:layout_alignParentLeft="true" 
      tools:layout="@android:layout/list_content" /> 

    </RelativeLayout> 

    <FrameLayout 
     android:id="@+id/action_detail_container" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="3" /> 

</LinearLayout> 

Я не знаю, как заполнить layout_height значения двух фрагментов. Я пробовал всевозможные ценности, но они, кажется, все должны быть абсолютными (чего я не хочу).

+1

добавить '' View' с андроида: layout_height = "0dip" и 'андроида: layout_centerVertical =" true' , затем выровняйте один из них над ним и один под ним с помощью 'android: layout_above =" "и' android: layout_above = "" '. – hypd09

+0

@ hypd09 Как бы я определил их для заполнения оставшегося пространства? чувствует себя немного взломанным. –

+0

просто установите 'android: layout_height =" match_parent "'. 'RelativeLayout' используется установкой отношений, ничего не взламывает об этом. – hypd09

ответ

7

Вот мое решение с общим обходного, чтобы избежать вложенную вес:

<LinearLayout 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" 
       android:layout_marginLeft="0dp" 
       android:layout_marginRight="0dp" 
       android:baselineAligned="false" 
       android:divider="?android:attr/dividerHorizontal" 
       android:orientation="horizontal" 
       android:showDividers="middle" 
       tools:context=".MainActivity" > 
    <RelativeLayout 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:orientation="vertical" 
      android:layout_weight="1" > 

     <View android:id="@+id/dummyView" 
       android:layout_width="0dp" 
       android:layout_height="0dp" 
       android:layout_centerInParent="true"/> 
     <fragment 
       android:id="@+id/fragment1" 
       android:name="com.example.Fragment1" 
       android:layout_width="wrap_content" 
       android:layout_height="fill_parent" 
       android:layout_alignTop="@id/dummyView" 
       tools:layout="@android:layout/list_content" /> 

     <fragment 
       android:id="@+id/fragment2" 
       android:name="com.example.Fragment2" 
       android:layout_width="wrap_content" 
       android:layout_height="fill_parent" 
       android:layout_alignBottom="@id/dummyView" 
       tools:layout="@android:layout/list_content" /> 

    </RelativeLayout> 

    <FrameLayout 
      android:id="@+id/action_detail_container" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="3" /> 

</LinearLayout> 
-1

попробовать этот путь

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 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" 
    android:baselineAligned="false" 
    android:divider="?android:attr/dividerHorizontal" 
    android:orientation="horizontal" 
    android:showDividers="middle" 
    tools:context=".MainActivity" > 
    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:orientation="vertical" 
     android:layout_weight="1" > 

     <fragment 
      android:id="@+id/fragment1" 
      android:name="com.example.Fragment1" 
      android:layout_width="wrap_content" 
      android:layout_height="0dp" 
      android:layout_weight="1" 
      tools:layout="@android:layout/list_content" /> 

     <fragment 
      android:id="@+id/fragment2" 
      android:name="com.example.Fragment2" 
      android:layout_width="wrap_content" 
      android:layout_height="0dp" 
      android:layout_marginTop="5dp" 
      android:layout_weight="1" 
      tools:layout="@android:layout/list_content" /> 

    </LinearLayout> 

    <FrameLayout 
     android:id="@+id/action_detail_container" 
     android:layout_width="0dp" 
     android:layout_marginLeft="5dp" 
     android:layout_height="match_parent" 
     android:layout_weight="3" /> 

</LinearLayout> 
+2

У этого есть вложенные веса, что плохо для производительности. –

+0

i don ' так думаю .. –

0

Почему бы не переключить его вокруг, то есть использовать RelativeLayout как ваш взгляд корня, и вертикальный LinearLayout в качестве контейнера для фрагментов? Просто используйте alignParentLeft на контейнере и toRightOf на FrameLayout.

+0

Могу ли я иметь два меньших фрагмента на 1/3 ширины и большой на 2/3? –

+0

О, я не знал, что вы хотели таких пропорций ... В этом случае решение @Andros правильное. – npace

0

Использование веса, как правило, только плохо сказывается на производительности, когда они вложены друг в друга (см this, например). Поэтому в вашем случае вы можете их использовать.
В противном случае, вы можете создать пустой взгляд в середине, чтобы помочь вам расположить другие:

<RelativeLayout 
    ... 
    > 
    <View 
    androi:id="@+id/separator" 
    android:layout_centerVertical="true" 
    android:layout_width="0" 
    android:layout_height="0" 
    > 
    <fragment 
    android:layout_above="@id/separator" 
    ... 
    > 
    <fragment 
    android:layout_below="@id/separator" 
    ... 
    > 
</RelativeLayout> 
Смежные вопросы