使用相对布局垂直均匀地分割两个片段



我正在尝试创建一个包含三个片段的简单布局。在左边,我想要两个片段,每个片段占据屏幕高度的50%。在右边,我想要一个大的容器片段,如下所示:

+-----+-----------------+
| 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值。我尝试了各种价值观,但它们似乎都必须是绝对的(我不想要)。

这是我

的解决方案,其中包含避免嵌套权重的常见解决方法:

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

为什么不切换它,即使用RelativeLayout作为根视图,垂直LinearLayout作为片段的容器?只需在容器上使用alignParentLeft,在FrameLayout上使用toRightOf即可。

使用权重在嵌套时通常不利于性能(例如,请参阅此内容)。因此,在您的情况下,您可以使用它们。
否则,您可以在中间创建一个空视图来帮助您定位其他视图:

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

最新更新