使用RelativeLayout均匀地垂直分割两个片段

时间:2014-01-17 09:49:37

标签: android android-layout android-fragments relativelayout

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

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

我使用LinearLayoutslayout_height="0dp"使用了两个layout_weight="1",但我得到的信息是性能不好,所以我开始使用{{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> 值。我尝试了各种各样的价值观,但它们似乎都必须是绝对的(我不想要)。

3 个答案:

答案 0 :(得分: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 :(得分:0)

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

答案 2 :(得分: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>
相关问题