带有缺口/显示切口的Android导航抽屉问题

时间:2018-12-01 20:11:06

标签: android android-navigationview display-cutouts

我正在使用Android Studio模板“ 导航抽屉活动”开发一个Android应用程序,但是如果我在带有Notch / display Cutouts(缺口/显示切口)的手机上打开导航抽屉,则状态栏盖会更大导航抽屉标题的顶部。

我该如何解决?

enter image description here

以下是“活动”布局xml:

<?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_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start"
    android:background="@color/colorPrimaryDark">

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

    <android.support.design.widget.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" />

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

以及导航抽屉标题布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="@dimen/nav_header_height"
    android:background="@drawable/navbar_size_edit"
    android:gravity="bottom"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin_bar_bottom"
    android:paddingLeft="@dimen/activity_horizontal_margin_bar"
    android:paddingRight="@dimen/activity_horizontal_margin_bar"
    android:paddingTop="@dimen/activity_vertical_margin_bar"
    android:theme="@style/ThemeOverlay.AppCompat.Dark">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@string/nav_header_desc"
        android:paddingTop="@dimen/nav_header_vertical_spacing"
        android:paddingBottom="@dimen/activity_vertical_margin_bar_bottom"
        app:srcCompat="@mipmap/avatar_me_round" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/nav_header_vertical_spacing"
        android:text="@string/app_name"
        android:textStyle="bold"
        android:textAppearance="@style/TextAppearance.AppCompat.Body1" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/app_version" />

</LinearLayout>

2 个答案:

答案 0 :(得分:1)

将android:fitsSystemWindows =“ true”的属性更改为android:fitsSystemWindows =“ false”。这就是对我有用的。

答案 1 :(得分:1)

要比 Adarsha Nayak V 更具体,请将android:fitsSystemWindows中的false设置为NavigationView

<androidx.drawerlayout.widget.DrawerLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            tools:openDrawer="start">

<!-- Other elements... -->

    <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="false"
                    app:headerLayout="@layout/navigation_drawer_header"
                    app:menu="@menu/navigation_drawer"/>
</androidx.drawerlayout.widget.DrawerLayout>