【问题标题】:How to add two fragments together in a frameLayout in runtime如何在运行时在 frameLayout 中将两个片段一起添加
【发布时间】:2013-07-13 20:58:46
【问题描述】:

我正在使用navigation drawer 开发一个 andorid 应用程序,并且我有一个这样的 contentview 布局:

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- As the main content view, the view below consumes the entire
         space available using match_parent in both dimensions. -->

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:dividerHeight="0dp"
        android:divider="@android:color/transparent"
        android:background="@color/default_color"/>
</android.support.v4.widget.DrawerLayout>

所以我可以在导航抽屉项目点击时添加和替换片段。到目前为止一切正常。

但在一种情况下,单击导航抽屉上的项目时,我需要显示两个片段而不是一个(就像 listFragment 和详细信息片段一样)。我该怎么做?我应该在框架布局中添加两个片段吗?但是怎么做?还是应该用不同的 drawerLayout 更改 contentView ?如果是,我该怎么做?

【问题讨论】:

    标签: android android-fragments android-framelayout navigation-drawer


    【解决方案1】:

    您可以只使用嵌套片段。创建另一个片段,它将动态创建两个片段并将它们添加到其布局中。

    更多信息请阅读:http://developer.android.com/about/versions/android-4.2.html#NestedFragments

    【讨论】:

    • 感谢您的建议。我从您的回答中了解到 - 我应该像以前一样每个导航项都有一个片段。其中一个有两个子片段。正确的?我也知道嵌套片段不支持从 UI 膨胀。那么我是否需要动态制作这些子片段?
    • 好吧,我接受了你的回答。但是我遇到了一个新问题并在这里打开了一个线程。请评论stackoverflow.com/questions/17638834/…
    • 那么问题是如何使用导航抽屉将两个片段用于平板电脑的一个活动和每个活动方法的一个片段?
    猜你喜欢
    • 1970-01-01
    • 2018-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-18
    • 1970-01-01
    相关资源
    最近更新 更多