DrawerLayout 中视图的错误大小和点击行为

Wrong size and click behavior for Views in DrawerLayout

我有一个 Activity 的布局,我正在尝试向其中添加导航抽屉。

问题是,要正常工作,我需要使用:

<android.support.v4.widget.DrawerLayout

而不是:

<RelativeLayout 

但它把事情搞砸了。我的 ProgressBar 变得更大,我的 RecyclerView 不工作,当我点击某些东西时应用程序将我注销等等。

我的布局XML:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.st.mf.UserAreaActivity"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:padding="@dimen/activity_vertical_margin"
    android:background="#fff">

    <ProgressBar
        android:id="@+id/progressBar1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_marginTop="20dp" />

    <android.support.design.widget.NavigationView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        app:menu="@layout/navigation_menu"
        android:layout_gravity="start">
    </android.support.design.widget.NavigationView>

</android.support.v4.widget.DrawerLayout>

如何创建我的抽屉菜单而不弄乱其他所有内容?

不是抽屉的 DrawerLayout 的任何直接子 View 都被视为内容 View,并且将在两个方向上布置到 match_parent,无论您在其上设置的宽度和高度属性。在你的情况下——事实上,在大多数情况下——你只需要一个内容 View,所以其余的非抽屉 Views 应该都在一个 ViewGroup.

我们会将您的 ProgressBarRecyclerView 都放在作为内容 ViewRelativeLayout 中,它们将保留您的布局属性已经设置。例如:

<android.support.v4.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"
    tools:context="com.st.mf.UserAreaActivity"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="@dimen/activity_vertical_margin"
    android:background="#fff">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ProgressBar
            android:id="@+id/progressBar1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true" />

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentTop="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentLeft="true"
            android:layout_marginTop="20dp" />

    </RelativeLayout>

    <android.support.design.widget.NavigationView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        app:menu="@layout/navigation_menu"
        android:layout_gravity="start">
    </android.support.design.widget.NavigationView>

</android.support.v4.widget.DrawerLayout>

请注意,内容 View 应始终列在任何抽屉之前,以保持正确的 z 顺序;即,让抽屉位于内容之上。