Android NavigationView(材料支持库)不能正确地与状态栏交互

Jin*_*Jin 17 android android-support-library material-design android-design-library

我正在按照这里的示例将新的Material Design Support库的NavigationView集成到我的应用程序中.

我的总体布局如下:

activity.xml

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

    <!-- main content -->

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

</android.support.v4.widget.DrawerLayout>
Run Code Online (Sandbox Code Playgroud)

的themes.xml

<style name="MyTheme" extends "Theme.AppCompat.Light.NoActionBar">
    <item name="android:statusBarColor">@android:color/transparent</item>
    <item name="android:windowDrawsSystemBarBackgrounds">true</item>
</style>
Run Code Online (Sandbox Code Playgroud)

MyActivity.java

onCreate(..) {
    ..
    // This color can be different depending on some conditions.
    DrawerLayout.setStatusBarBackground(color);
}
Run Code Online (Sandbox Code Playgroud)

但是,我不断获得灰色状态栏,并且NavigationView不会在状态栏下绘制.我认为灰色状态栏是因为我没有colorPrimaryDark在主题中定义自定义attr.但是,我认为DrawerLayout.setStatusBarBackground会覆盖并设置状态栏颜色.我找不到太多关于新文档的文档NavigationView.有没有人有任何想法?

hun*_*ost 7

在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>
Run Code Online (Sandbox Code Playgroud)

在activity.xml中,尝试设置android:fitsSystemWindows="true"您的NavigationView(除了DrawerLayout).


Jin*_*Jin 3

叹息,我找到了我的问题。我们使用一些奇怪的抽象来在右侧添加调试抽屉。结果,整体视图层次结构实际上看起来像这样

<DrawerLayout id="debug">

    <LinearLayout id="debug_drawer" />

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

        <NavigationView .... />

    </DrawerLayout>

</DrawerLayout>
Run Code Online (Sandbox Code Playgroud)

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