安卓:当/我为什么要使用的FrameLayout,而不是片段? [英] Android: when / why should I use FrameLayout instead of Fragment?

查看:179
本文介绍了安卓:当/我为什么要使用的FrameLayout,而不是片段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立了大屏幕的布局,这应该是由2个不同的部分,左边的和正确的。对于这样做,我想用2片段是正确的选择。

I am building a layout for large screens, that is supposed to consist of 2 different parts, a left one and a right one. For doing that I thought using 2 Fragments is the right choice.

然后,我对导航与主/从流量的例子来看看。它有一个2 - 面板布局,其中,右边是导航,并在左边是详细信息视图。

Then I had a look on the example of the navigation with the Master/Detail-Flow. It has a 2-pane layout, where on the right is the navigation, and on the left is the detail view.

但在这个例子中,从我期望看到,在详细视图有一个的FrameLayout 这则持有片段,而不是片段直接

But in that example, different from what I expected to see, for the detail view there is a FrameLayout that then holds a Fragment, instead of a Fragment directly.

布局XML看起来像这样(的例子):

The layout XML looks like this (an example):

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

我现在的问题是:为什么一个的FrameLayout 代替片段本身的细节视图?这是什么原因或者优势?我应该使用它吗?

My question now is: why is a FrameLayout used instead of the Fragment itself for the detail view? What is the reason or the advantage? Should I use it too?

推荐答案

细节容器是的FrameLayout ,因为片段则显示的使用被替换 FragmentTransaction 更换()方法。

The detail container is a FrameLayout because the Fragment that is displayed will be replaced using FragmentTransaction's replace() method.

第一个参数更换()是集装箱的ID,其碎片将被替换。如果的FrameLayout在这个例子中换成了一个片段,那么无论是 WorkStationListFragment 和任何细节片段目前显示会被新的片段所取代。通过一个的FrameLayout内封装的片段,你可以只更换细节。

The first argument to replace() is the ID of container whose Fragments will be replaced. If the FrameLayout in this example were replaced with a Fragment, then both the WorkStationListFragment and whatever detail Fragment is currently shown would be replaced by the new Fragment. By encapsulating the Fragment within a FrameLayout, you can replace just the details.

这篇关于安卓:当/我为什么要使用的FrameLayout,而不是片段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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