使用RelativeLayout的均匀垂直拆分两个片段 [英] Using a RelativeLayout to vertically split two fragments evenly

查看:137
本文介绍了使用RelativeLayout的均匀垂直拆分两个片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用三个片段创建一个简单的布局。在左边,我想对海誓山盟两个片段,画面高度的每次服用50%。在右边,我想要一个大型集装箱的片段,像这样的:

I am trying to create a simple layout with three fragments. On the left, I want two fragments over eachother, each taking 50% of the height of the screen. On the right, I want one large container fragment, like this:

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

我得到了它的工作两个 LinearLayouts ,使用 layout_height =0dp layout_weight =1,但我得到了它是坏的性能信息,所以我开始使用 RelativeLayout的。我现在是这样的:

I got it working with two LinearLayouts, using layout_height="0dp" and layout_weight="1", but I got the message it is bad for performance, so I set out to use a RelativeLayout. What I have now is this:

<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 填入值。我尝试了所有类型的值,但他们似乎都不得不是绝对的(我不希望)。

I don't know how to fill in the layout_height value of the two fragments. I tried all kinds of values, but they seem all to have to be absolute (which I don't want).

推荐答案

下面是我的一个共同的解决方法解决方案,以避免嵌套的重量:

Here is my solution with a common workaround to avoid nested weight :

<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的均匀垂直拆分两个片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆