我正在尝试创建一个包含三个 fragment 的简单布局。在左边,我想要两个 fragment 彼此重叠,每个 fragment 占据屏幕高度的 50%。在右侧,我想要一个大容器 fragment ,如下所示:
+-----+-----------------+
| f1 | detail_container|
| | |
+-----+ |
| f2 | |
| | |
+-----+-----------------+
我使用 layout_height="0dp"
和 layout_weight="1"
使用两个 LinearLayouts
,但我收到了消息这对性能不利,所以我开始使用 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>
两个 fragment 的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>
关于android - 使用 RelativeLayout 垂直平均分割两个 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21182702/