Android DrawerLayout 问题

如题,我想实现滑动菜单功能
这是我的布局代码:


<androidx.drawerlayout.widget.DrawerLayout
    android:id="@+id/drawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            />

        <Button
            android:id="@+id/learnNew"
            android:layout_width="133dp"
            android:layout_height="63dp"
            android:layout_marginStart="64dp"
            android:layout_marginTop="232dp"
            android:text="学习"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/userWelcome"
            android:layout_width="173dp"
            android:layout_height="52dp"
            android:layout_marginStart="52dp"
            android:layout_marginTop="96dp"
            android:text="TextView"
            android:textSize="34sp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    androidx.constraintlayout.widget.ConstraintLayout>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="start"
        android:background="#FFF"
        android:text="This is menu"
        android:textSize="30sp"
        />
androidx.drawerlayout.widget.DrawerLayout>

但是显示出来的却是这个样子:

img

划也划不动,不过还能点击到被遮盖的按钮,这是怎么回事?

将侧滑菜单的内容放在一个布局中试试看:

<androidx.drawerlayout.widget.DrawerLayout
    android:id="@+id/drawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <!-- 在这里放置你的主界面布局 -->

    </androidx.constraintlayout.widget.ConstraintLayout>

    <!-- 侧滑菜单布局 -->
    <LinearLayout
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@android:color/white"
        android:orientation="vertical">

        <!-- 在这里放置你的侧滑菜单布局 -->

    </LinearLayout>

</androidx.drawerlayout.widget.DrawerLayout>