Java 如何在执行特定操作时显示片段

Java 如何在执行特定操作时显示片段,java,android,android-fragments,fragmenttransaction,Java,Android,Android Fragments,Fragmenttransaction,实际上,我正在开发一个有导航抽屉的应用程序,我想使用片段而不是单独的活动。 我想知道怎么做? 提前感谢。检查此示例代码。希望这对您有所帮助 <?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schem

实际上,我正在开发一个有导航抽屉的应用程序,我想使用片段而不是单独的活动。 我想知道怎么做?
提前感谢。

检查此示例代码。希望这对您有所帮助

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    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:fitsSystemWindows="true"
    tools:openDrawer="start">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <include layout="@layout/toolbar_layout">
        <!--The Toolbar Layout if you have toolbar-->
    </include>

    <FrameLayout
        android:id="@+id/base_container"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <!--The layout where you need to add different
            fragments on click of nabigation items-->

    </FrameLayout>

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:orientation="vertical">

 <!--The navigation drawer items-->
</LinearLayout>


将框架布局添加到navigationdrawer活动中,制作片段,然后使用片段管理器显示……请参阅@SushilDubey。实际上,我创建了一个片段及其布局文件,我将其包含在主要活动的布局文件中,并创建了片段管理器,片段事务开始。。等,但片段中的内容将直接显示,而不考虑任何操作。只有在执行action@AyushKhare是的,我也做了同样的事情,当用户在导航抽屉中选择一个选项时,应该调用这个片段。