我正在关注here中的示例,将新的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>
的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>
MyActivity.java
onCreate(..) {
..
// This color can be different depending on some conditions.
DrawerLayout.setStatusBarBackground(color);
}
但是,我一直收到一个灰色状态栏,NavigationView
没有在状态栏下绘制。我认为灰色状态栏是因为我没有在主题中定义自定义colorPrimaryDark
attr。但是,我认为DrawerLayout.setStatusBarBackground
会覆盖并设置状态栏颜色。我找不到关于新NavigationView
的更多文档。有没有人有任何想法?
答案 0 :(得分: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>
在activity.xml中,尝试在android:fitsSystemWindows="true"
上设置NavigationView
(除了DrawerLayout
)。
答案 1 :(得分:4)
对于每一个苦苦挣扎的人来说,你的布局文件应该是这样的:
<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"
在DrawerLayout和NavigationView中。
答案 2 :(得分:3)
叹了口气,我发现了我的问题。我们使用一些奇怪的抽象来在右侧添加调试抽屉。因此,整体视图层次结构实际上看起来像这样
<DrawerLayout id="debug">
<LinearLayout id="debug_drawer" />
<DrawerLayout id="nav_drawer" fitsSystemWindows="true">
<NavigationView .... />
</DrawerLayout>
</DrawerLayout>
所以我在错误的抽屉上调用DrawerLayout.setStatusBarColor
并在错误的抽屉上设置fitsSystemWindows
。因此,窗口永远不会与状态栏交互:(