【问题标题】:Using a RelativeLayout to vertically split two fragments evenly使用RelativeLayout 将两个片段垂直均匀分割
【发布时间】:2014-02-06 14:16:28
【问题描述】:

我正在尝试创建一个包含三个片段的简单布局。在左边,我想要两个片段彼此重叠,每个片段占据屏幕高度的 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值不知道怎么填。我尝试了各种值,但它们似乎都必须是绝对的(我不想要)。

【问题讨论】:

  • 添加Viewandroid:layout_height="0dip"android:layout_centerVertical="true,然后使用android:layout_above="&lt;id&gt;"android:layout_above="&lt;id&gt;" 将其中一个对齐在其上方和下方对齐。
  • @hypd09 我将如何定义它们以填充剩余空间?另外,感觉有点hackish。
  • 只需设置android:layout_height="match_parent"RelativeLayout 用于设置关系,这没什么不好。

标签: android android-layout android-fragments android-relativelayout


【解决方案1】:

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

【讨论】:

  • 我能否将两个较小的片段放在宽度的 1/3 处,将较大的片段放在 2/3 处?
  • 哦,我不知道你想要这样的比例......在这种情况下,@Andros 的解决方案是正确的。
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

    在嵌套时使用权重通常对性能不利(例如,参见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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-01-05
      • 2012-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多