onNavigationItemSelected 打开activityXML 问题

onNavigationItemSelected open activity XML problem

原来的菜单项无法打开的问题 activity 已通过删除所有 XML 包含 ConstraintLayout 的代码得到解决。在 ConstraintLayout 中创建所有内容后,我添加了 NavigationDrawer。我怎样才能让 NavigationDrawer 和我自己的东西一起工作?

<?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:background="@drawable/main_screen_image"
    android:fitsSystemWindows="true"
    tools:context=".HomeScreen"
    tools:openDrawer="start">

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

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/purple_200"
            android:elevation="4dp"
            android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
        <!--Dark text
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"-->

        <!--        Container for fragments-->
        <!--<FrameLayout
            android:id="@+id/fragment_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />-->

    </LinearLayout>

    <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"
        app:headerLayout="@layout/nav_header"
        app:menu="@menu/drawer_menu" />

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:padding="20dp">

        <ImageView
            android:id="@+id/homeScreenLogo"
            android:layout_width="160dp"
            android:layout_height="160dp"
            android:layout_gravity="center"
            android:src="@drawable/kaznau_logo"
            app:layout_constraintBottom_toTopOf="@+id/guideline6"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.498"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <LinearLayout
            android:id="@+id/linearLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="50dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.4"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="@+id/guideline6">

            <com.google.android.material.button.MaterialButton
                android:id="@+id/btnAboutUniversity"
                android:layout_width="160dp"
                android:layout_height="50dp"
                android:drawableLeft="@drawable/ic_university"
                android:text="@string/s_about_kaznau"
                app:cornerRadius="5dp" />

            <View
                android:layout_width="0dp"
                android:layout_height="20dp"
                android:layout_weight="1" />

            <com.google.android.material.button.MaterialButton
                android:id="@+id/btnFaculties"
                android:layout_width="160dp"
                android:layout_height="50dp"
                android:drawableLeft="@drawable/ic_faculty"
                android:text="@string/s_faculty"
                app:cornerRadius="5dp" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/linearLayout2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/linearLayout">

            <com.google.android.material.button.MaterialButton
                android:id="@+id/btnNews"
                android:layout_width="160dp"
                android:layout_height="50dp"
                android:drawableLeft="@drawable/ic_baseline_article_24"
                android:text="@string/s_news"
                app:cornerRadius="5dp" />

            <View
                android:layout_width="0dp"
                android:layout_height="20dp"
                android:layout_weight="1" />

            <com.google.android.material.button.MaterialButton
                android:id="@+id/btnEvents"
                android:layout_width="160dp"
                android:layout_height="50dp"
                android:drawableLeft="@drawable/ic_baseline_event_24"
                android:text="@string/events_name"
                app:cornerRadius="5dp" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/linearLayout3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/linearLayout2">

            <com.google.android.material.button.MaterialButton
                android:id="@+id/btnLocations"
                android:layout_width="160dp"
                android:layout_height="50dp"
                android:drawableLeft="@drawable/ic_location"
                android:text="@string/s_locations"
                app:cornerRadius="5dp" />

            <View
                android:layout_width="0dp"
                android:layout_height="20dp"
                android:layout_weight="1" />

            <com.google.android.material.button.MaterialButton
                android:id="@+id/btnCampus"
                android:layout_width="160dp"
                android:layout_height="50dp"
                android:drawableLeft="@drawable/ic_campus"
                android:text="@string/s_campus"
                app:cornerRadius="5dp" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/linearLayout4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/linearLayout3">

            <com.google.android.material.button.MaterialButton
                android:id="@+id/btnLinks"
                android:layout_width="160dp"
                android:layout_height="50dp"
                android:drawableLeft="@drawable/ic_links"
                android:text="@string/s_links"
                app:cornerRadius="5dp" />

            <View
                android:layout_width="0dp"
                android:layout_height="20dp"
                android:layout_weight="1" />

            <com.google.android.material.button.MaterialButton
                android:id="@+id/btnContacts"
                android:layout_width="160dp"
                android:layout_height="50dp"
                android:drawableLeft="@drawable/ic_contacts"
                android:text="@string/s_contacs"
                app:cornerRadius="5dp" />
        </LinearLayout>

        <com.google.android.material.button.MaterialButton
            android:id="@+id/btnAgrodamu"
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:layout_gravity="center"
            android:text="@string/s_agrodamu"
            app:cornerRadius="5dp"
            app:layout_constraintBottom_toTopOf="@+id/guideline7"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.555"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/linearLayout4"
            app:layout_constraintVertical_bias="0.164" />

        <androidx.constraintlayout.widget.Guideline
            android:id="@+id/guideline6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            app:layout_constraintGuide_begin="20dp"
            app:layout_constraintGuide_percent="0.4" />

        <androidx.constraintlayout.widget.Guideline
            android:id="@+id/guideline7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            app:layout_constraintGuide_begin="20dp"
            app:layout_constraintGuide_percent="0.9" />
    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.drawerlayout.widget.DrawerLayout>

如果您还可以告诉我如何使我的 XML 代码更简洁,同时又不丢失其功能和外观,我将不胜感激。谢谢

好的,我明白了。在关闭 DrawerLayout 标签之前,您必须将 NavigationView 放在底部