Android 使用RelativeLayout垂直均匀分割两个碎片
我试图创建一个包含三个片段的简单布局。在左边,我想要两个碎片,每个碎片占据屏幕高度的50%。在右边,我想要一个大容器片段,如下所示:Android 使用RelativeLayout垂直均匀分割两个碎片,android,android-layout,android-fragments,android-relativelayout,Android,Android Layout,Android Fragments,Android Relativelayout,我试图创建一个包含三个片段的简单布局。在左边,我想要两个碎片,每个碎片占据屏幕高度的50%。在右边,我想要一个大容器片段,如下所示: +-----+-----------------+ | f1 | detail_container| | | | +-----+ | | f2 | | | | | +-----+-----------------+
+-----+-----------------+
| f1 | detail_container|
| | |
+-----+ |
| f2 | |
| | |
+-----+-----------------+
我使用了两个线性布局
,使用了布局_height=“0dp”
和布局_weight=“1”
,但我得到的信息是,这对性能有害,所以我开始使用相对布局
。我现在得到的是:
<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\u height
值。我尝试了所有类型的值,但它们似乎都必须是绝对值(我不想要)。为什么不将其切换,即使用RelativeLayout
作为根视图,使用垂直线性布局作为片段的容器?只需在容器上使用alignParentLeft
,在FrameLayout
上使用toRightOf
,以下是我的解决方案,其中有一个常见的解决方法,可以避免嵌套重量:
<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
...
>
<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>
使用android:layout\u height=“0dip”
和android:layout\u centerVertical=“true
添加一个视图
,然后使用android:layout\u over=“/code>和android:layout\u over=”将其中一个视图在视图上方和下方对齐“
@hypd09如何定义它们以填充剩余空间?另外,它感觉有点黑客味。只需设置android:layout\u height=“match\u parent”
RelativeLayout
用于设置关系,这一点也不奇怪。我能把两个较小的片段放在宽度的1/3,把较大的片段放在2/3吗?哦,我不知道你想要这样的比例。。。在这种情况下,@Andros的解决方案是正确的。