将NavigationView的标题栏绘制在状态栏下方,同时保持NavigationView在状态栏后面。

12

我一直在尝试将NavigationView的头部绘制在状态栏下方,但它始终停留在状态栏后面。

我已经为我的DrawerLayout和NavigationView都设置了属性android:fitsSystemWindows="true",但是没有效果。

以下是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/activity_dashboard"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?attr/colorPrimary"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_scrollFlags="scroll|enterAlways|snap">

            </android.support.v7.widget.Toolbar>

            <android.support.design.widget.TabLayout
                android:id="@+id/tab"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:tabMode="scrollable">

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

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

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

    <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:background="@android:color/white"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header" />

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

当前的外观


你有没有找到解决方案? - Ray
1
@RayKoopa 将 android:fitsSystemWindows="false" 这行添加到抽屉布局中。 - bebosh
3个回答

13

我曾经遇到过同样的问题,在您的NavigationView中将fitsSystemWindows设置为false,其余保持不变。 从我所理解的API >= 21开始,NavigationView会落在状态栏后面。


2
这样做会使NavigationView出现在状态栏下方。我不想要这种行为。我希望NavigationView在状态栏后面绘制,但标题应该在状态栏下方。 - Asad Tariq

8

你尝试过在标题视图上将 setFitsSystemWindows 设置为 false 吗?

如果你尝试了但没有成功,你可以通过观察标题视图或 NavigationView 上的 onApplyWindowInsetsListener,手动为视图设置顶部内边距。

 ViewCompat.setOnApplyWindowInsetsListener(headerView) { view, insets ->
      view.setPadding(0, insets.systemWindowInsetTop, 0, 0)
      insets
 }

这对我很有效。我需要保持setFitsSystemWindows为true以满足其他需求。谢谢! - John Oberhauser
这应该是被接受的答案,像魅力一样工作。在包含标题的导航视图中添加了setFitsSystemWindows设置为false,并在抽屉布局中添加了另一个setFitsSystemWindows设置为true。 - Acemond

2
你尝试在标题布局的根节点中添加 android:fitsSystemWindows="true" 吗?这对我有用。

你可以看到他的代码中有这个,导致他的导航视图在状态栏下面,而不是下方。 - Ray
@RayKoopa 不行。你只能看到对头部布局的引用 app:headerLayout="@layout/nav_header",而不能看到头部本身的内容。在头部的根视图上添加 android:fitsSystemWindows="true" 也对我很有帮助。 - Catalin Morosan
似乎我的设备和你的设备有不同的行为,对我来说它必须是“false”。 - Ray

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接