繁体   English   中英

尝试显示不在菜单中的片段时,导航抽屉活动项目中的片段重叠

[英]Fragments overlapping in Navigation Drawer Activity project when trying to display a fragment that isn't in the menu

我使用“导航抽屉活动”模板创建了一个简单的项目。
当我尝试导航到不在导航菜单中的片段时,片段出现在当前片段的顶部。 在此处输入图像描述
我使用它是为了导航到不在导航菜单中的片段我使用它:

FragmentManager fm = getActivity().getSupportFragmentManager();
fm.beginTransaction().replace(R.id.nav_host_fragment, nextFrag).commit();

所有 app_bar_main 和 content_main xml 文件与 android studio 生成的文件完全相同。

我使用上面的“nav_host_fragment”,因为在我的 content_main 文件中我有:

<androidx.constraintlayout.widget.ConstraintLayout 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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/app_bar_main">

    <fragment
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/mobile_navigation" />
</androidx.constraintlayout.widget.ConstraintLayout>

我尝试在它周围包裹另一个容器并使用它,但没有任何改变。

在使用NavHostFragment和导航组件时,您永远不应该使用FragmentTransaction (事实上,即使您不使用它们,也永远无法替换<fragment>添加的片段。

Instead, you should follow the Tie destinations to menu items and add your fragment to your res/navigation/mobile_navigation.xml file and make sure that the ID of your menu.xml file item matches the android:id of the destination in your graph.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM