为什么导航抽屉活动不起作用?

问题描述:

使用Navigation Drawer Activity 创建新项目后,构建并运行,该Activity 不起作用.它有许多片段,当从活动抽屉中选择它们时,它们应该更改为.打开抽屉,选择选择,就看到:这是家片段.

After creating a new project with Navigation Drawer Activity, build and run, the activity does not work. It has a number of fragments that it is supposed to change to when they are selected from the activity drawer. Open drawer, select choice, just see: This is home fragment.

2 小时上网搜索,答案不清楚.DrawerLayout 的 Z-Order

2 hours searching web, answer is not clear. Z-Order of DrawerLayout

我只是希望模板在您运行时能够实际工作.

I Just expect the template to actually work when you run it.

所以,为了让导航抽屉活动(新的模板...您可以从中创建一个新项目)工作,您必须通过移动

So, just to get the Navigation Drawer Activity (template in new... that you can create a new project from) to work at all, you have to edit the activity_main.xml by moving the

<include
        layout="@layout/app_bar_main"

以上

<com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"

然后,程序将运行,您可以从那里继续.
所以改变:

then, the program will work and you can carry on from there.
so change:

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout 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/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />

    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</androidx.drawerlayout.widget.DrawerLayout>

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout 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/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />

</androidx.drawerlayout.widget.DrawerLayout>

我希望这可以帮助那些没有我做那么多时间的人.这里的教训是:仅仅因为开发人员提供了一个模板,并不意味着它可以正常工作.所以不要自动认为程序不工作是你的错.

I hope this helps someone not waist as much time as I did. The lesson here is: just because the developers provide a template, doesn't mean it works correctly. So don't automatically assume that it's your fault the program doesn't work.