【问题标题】:How can one inflate 2 fragment views from hosting activity at the same time?一个人如何同时从托管活动中膨胀 2 个片段视图?
【发布时间】:2017-11-18 08:53:11
【问题描述】:

我在片段的主要活动中有这段代码。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/mainView"
    android:orientation="vertical"
    android:layout_marginBottom="10dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:id="@+id/mainView2"/>


</LinearLayout>

我在我的主要活动中使用此代码来膨胀片段

FragmentManager fm = getSupportFragmentManager();
        fm.beginTransaction().add(R.id.mainView,new MainViewFragment())
                .add(R.id.mainView2,new SecondProductLayout())
                .commit();

但我仍然得到第一个片段视图,我做错了什么,我该如何解决这个问题?

【问题讨论】:

  • 为什么mainView2在mainView里面? mainView2 不应该低于 mainView 吗?
  • @ChinLoong 理论上应该,但它会抱怨多个根标签。

标签: android android-fragments android-activity


【解决方案1】:

将活动屏幕分成两部分,在你的活动中膨胀你的片段

【讨论】:

    【解决方案2】:
    【解决方案3】:

    您必须像往常一样使用权重将布局分成两部分并膨胀两个片段。

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                 xmlns:tools="http://schemas.android.com/tools"
                 android:id="@+id/frameContainer"
                 android:layout_width="match_parent"
                 android:layout_height="match_parent"
                  android:orientation="vertical"
                 tools:context="com.android.buffer.exampleapplication.MainActivity">
    
        <FrameLayout
            android:id="@+id/flFragment1"
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    
          <FrameLayout
              android:id="@+id/flFragment2"
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    
    </LinearLayout>
    

    java 代码膨胀片段。

    getFragmentManager().beginTransaction().replace(R.id.flFragment1, FragmentA.getInstance(msg)).commit();
                getFragmentManager().beginTransaction().replace(R.id.flFragment2, FragmentA.getInstance(msg)).commit();
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-30
    • 1970-01-01
    • 2020-04-01
    • 1970-01-01
    相关资源
    最近更新 更多