关于导航片段

时间:2021-01-18 14:04:14

标签: android kotlin fragment

NavgationHostFragment 不起作用。

//activity_main
<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context=".MainActivity">


    <fragment
        android:id="@+id/fragment3"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:navGraph="@navigation/nav_main"
        tools:layout_editor_absoluteX="131dp"
        tools:layout_editor_absoluteY="201dp" />

</androidx.constraintlayout.widget.ConstraintLayout>


//nav_main
<?xml version="1.0" encoding="utf-8"?>
<navigation 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:id="@+id/nav_main"
    app:startDestination="@id/welcomeFragment2">
    <fragment
        android:id="@+id/welcomeFragment2"
        android:name="com.example.fragmenttest.WelcomeFragment"
        android:label="WelcomeFragment" />
</navigation>

我不知道为什么。

Fragment 或其他文档中没有描述该过程。

请给我一个提示,如何让 Fragment xml 显示而不是 Activity_main。 我已经为此苦苦挣扎了好几天。 感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

对不起,我没有添加适配器,它没有显示出来。