NavigationView 布局中的负边距顶部

negative margin top in NavigationView layout

我有使用以下代码的自定义 DrawerLayout。 我不知道为什么我的布局 (drawer_layout) 从顶部开始留白,您在下面看到的红色关闭按钮被覆盖了。

<?xml version="1.0" encoding="utf-8"?>
<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"
android:id="@+id/drawer"
tools:context=".DashboardActivity"
android:layout_gravity="left"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:openDrawer="start">

[Some other elements here ...]


<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:layout_marginRight="-64dp"
    android:fitsSystemWindows="true">

<include layout="@layout/drawer_layout" />

</android.support.design.widget.NavigationView>

这是我的 NavigationView 自定义布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:background="@color/primary_color"
android:layout_height="match_parent">

<ImageView
     [Properties]
 />

<TextView

    [Properties]
    />

<TextView

    [Properties]
    />

<ImageView
     [Properties]
 />

<ImageButton
    android:id="@+id/drawer_close_image_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:layout_marginTop="8dp"
    android:background="@drawable/radius_background_acent_color"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:srcCompat="@drawable/ic_close_black_24dp" />

</android.support.constraint.ConstraintLayout>

但结果我有一个负边距顶部,可以在下图中看到。

如何删除顶部的边距并正确显示我的 NavigationView 布局?

删除 android:fitsSystemWindows="true"。该行告诉布局引擎使用整个屏幕放置元素,包括状态栏下方的位。

我认为 android:fitsSystemWindows="true" 导致了您的问题。

尝试删除此行