我正在为大屏幕构建一个布局,它应该由 2 个不同的部分组成,一个左侧和一个右侧。为此,我认为使用 2 Fragments 是正确的选择。
然后我看了一下使用 Master/Detail-Flow 的导航示例。它有一个 2 Pane 布局,右侧是导航,左侧是详细 View 。
但在该示例中,与我预期的不同,对于详细 View ,有一个 FrameLayout
然后保存一个 Fragment
,而不是一个 直接 fragment
。
布局 XML 如下所示(示例):
<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="16dp"
android:layout_marginRight="16dp"
android:baselineAligned="false"
android:divider="?android:attr/dividerHorizontal"
android:orientation="horizontal"
android:showDividers="middle"
tools:context=".WorkStationListActivity" >
<fragment
android:id="@+id/workstation_list"
android:name="de.tuhh.ipmt.ialp.history.WorkStationListFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
tools:layout="@android:layout/list_content" />
<FrameLayout
android:id="@+id/workstation_detail_container"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3" />
</LinearLayout>
我现在的问题是:为什么在详细 View 中使用 FrameLayout
而不是 Fragment
本身?原因或优势是什么?我也应该使用它吗?
最佳答案
详细信息容器是一个FrameLayout
,因为显示的Fragment
将使用FragmentTransaction
的replace()
替换方法。
replace()
的第一个参数是要替换 Fragments 的容器的 ID。如果此示例中的 FrameLayout 被替换为 Fragment,则 WorkStationListFragment
和当前显示的任何详细 Fragment 都将被新的 Fragment 替换。通过将 Fragment 封装在 FrameLayout 中,您可以只替换细节。
关于Android:何时/为什么我应该使用 FrameLayout 而不是 Fragment?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19453530/