如何使用FragmentManager在FrameLayout中显示片段A,B,C,...?

Waz*_*_Be 8 layout android replace fragment

亲爱的StackOverflow人,

我目前在Android应用程序中存在一个大问题.

我正在使用Fragments进行所有活动,我在这里阅读了所有文档:http://developer.android.com/guide/topics/fundamentals/fragments.html

我的应用程序现在在手机和平​​板电脑上看起来很酷.

我的问题:

我有一个显示FrameLayout中包含的Fragments的布局(参见底部的截图)

所以,这是片段A的截图.

现在,我想点击左键,将片段A替换为片段B或C,或者......

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <fragment android:name="com.stackoverflow.question.FRAGMENT_A"
            android:id="@+id/list"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="match_parent" />
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)

正如您在我的xml中看到的那样,FRAGMENT_A是硬编码的.

想象一下,我想在6个不同的片段之间切换,我做什么?将我的所有6个片段放在XML中,或者是否有一种方法可以用FRAGMENT_B,C,D,E等以编程方式替换FRAGMENT_A.

非常感谢你的帮助.

在此输入图像描述

Yas*_*mar 14

使用FragmentTransaction替换片段.

你可以以编程方式替换片段.

FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
FragmentB fb = new FragmentB();
ft.replace(R.id.list, fb);
ft.addToBackStack("replacingFragmentA");
ft.commit();
Run Code Online (Sandbox Code Playgroud)

添加到后台堆栈是可选的.