Android NavigationView(材料支持库)没有';t与状态栏正确交互

Android NavigationView(材料支持库)没有';t与状态栏正确交互,android,android-support-library,material-design,android-design-library,Android,Android Support Library,Material Design,Android Design Library,我正在按照中的示例将新材质设计支持库集成到我的应用程序中 我的总体布局如下所示: <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-au

我正在按照中的示例将新材质设计支持库集成到我的应用程序中

我的总体布局如下所示:

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    ...

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_height="match_parent"
        android:layout_width="wrap_content"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/drawer_header"
        app:menu="@menu/menu_drawer"/>

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

<android.support.v4.widget.DrawerLayout
    android:fitsSystemWindows="true">

    <!-- main content -->

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

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

但是,我一直得到一个灰色的状态栏,
NavigationView
不会在状态栏下绘制。我认为灰色状态栏是因为我没有在主题中定义自定义的
colorPrimaryDark
attr。但是,我假设
DrawerLayout.setStatusBarBackground
将覆盖并设置状态栏颜色。我在新的
导航视图
上找不到太多文档。有人有什么想法吗?

在values-v21/themes.xml中添加API 21+的样式:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="MyTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:statusBarColor">@android:color/transparent</item>
        <item name="android:windowDrawsSystemBarBackgrounds">true</item>
    </style>
</resources>

@android:彩色/透明
真的

在activity.xml中,尝试在
导航视图上设置
android:fitsystemwindows=“true”
(除了
DrawerLayout
)。

唉,我解决了我的问题。有一些奇怪的抽象,我们用来在右边添加一个调试抽屉。因此,整体视图层次结构实际上如下所示

<DrawerLayout id="debug">

    <LinearLayout id="debug_drawer" />

    <DrawerLayout id="nav_drawer" fitsSystemWindows="true">

        <NavigationView .... />

    </DrawerLayout>

</DrawerLayout>


因此,我在错误的抽屉上调用了
DrawerLayout.setStatusBarColor
,并在错误的抽屉上设置了
fitsSystemWindows
。因此,该窗口永远不会与状态栏交互:(

对于每一个与此相关的用户,您的布局文件应该如下所示:

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    ...

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_height="match_parent"
        android:layout_width="wrap_content"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/drawer_header"
        app:menu="@menu/menu_drawer"/>

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

在抽屉布局和导航视图中。

我知道这是一种糟糕的样式,但这并不重要,因为在21之前的版本中,无法识别的属性名称将被忽略。因此,将样式移动到-v21不会改变任何内容:(你确定你的
导航视图
具有
android:fitsSystemWindows=“true”吗
也是?我尝试在
导航视图上添加属性,但没有任何效果。状态栏保持灰色和不透明。这几乎就像
android:windowDrawsSystemBarBackgrounds
没有做任何事情,因为
抽屉布局。setStatusBarBackground
没有更改状态栏的颜色…
抽屉Layout.setStatusBarBackground(color)
应该抛出一个
NotFoundException
。你应该改为使用
DrawerLayout.setStatusBarBackgroundColor(color)
。或者这只是问题中的一个错误?感谢所有的帮助。这是问题中的伪代码/泛化。我终于解决了我的问题:(
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    ...

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_height="match_parent"
        android:layout_width="wrap_content"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/drawer_header"
        app:menu="@menu/menu_drawer"/>

</android.support.v4.widget.DrawerLayout>
android:fitsSystemWindows="true"