在导航抽屉中着色Android状态栏

And*_*rew 8 android android-appcompat android-theme navigation-drawer android-5.0-lollipop

在我正在构建的这个应用程序中,我在我的活动中添加了一个导航抽屉片段.我使用5.0,所以我已经能够设置primaryColor和primaryColorDark以获得正确的颜色.我决定尝试将我的Nav Drawer设计为与5.0中的Google Now抽屉非常相似,其中Nav Drawer拥有自己的状态栏背景.(不能张贴图片,信誉不够>.>)

我在这里已经按照这个问题提出了建议,这有很大帮助.当我把它拉出来时,我已经在状态栏下绘制了我的导航抽屉.唯一的麻烦是,我无法弄清楚如何设置抽屉中显示的状态栏的颜色.目前,它只显示白色.

以下是我的样式和代码的相关部分:

活动布局:

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

    <!-- main content -->
    ...

    <!-- Drawer fragment -->
    <fragment
        android:id="@+id/navigation_drawer"
        android:layout_width="@dimen/navigation_drawer_width"
        android:layout_height="match_parent"
        android:layout_gravity="left|start"
        android:theme="@style/DrawerTheme"
        android:fitsSystemWindows="true"
        android:choiceMode="singleChoice"
        android:name="com.myname.appname.NavigationDrawerFragment"
        tools:layout="@layout/fragment_navigation_drawer" />

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

片段布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/navDrawerFragment"
    android:theme="@style/DrawerTheme"
    android:background="@color/WindowBackground"
    tools:context=".NavigationDrawerFragment"
    android:fitsSystemWindows="true">

     <!-- content here -->

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

上面的每个链接,为活动设置状态栏的颜色(此部分正常工作):

public void onCreate(Bundled savedInstanceState) {
    super.onCreate(savedInstanceState);

    // ....

    drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawerLayout.setDrawerShadow(R.drawable.drawer_shadow, Gravity.START);
    drawerLayout.setStatusBarBackgroundColor(getResources().getColor(R.color.colorPrimaryDark));

    // ....
}
Run Code Online (Sandbox Code Playgroud)

最后,我为活动创建了相关的样式,以及我试图分配给片段的样式.

<style name="StatusBarActivityTheme" parent="MyAppTheme">
    <item name="android:windowDrawsSystemBarBackgrounds">true</item>
    <item name="android:statusBarColor">@android:color/transparent</item>
</style>


<style name="DrawerTheme" parent="MyAppTheme">
    <item name="android:statusBarColor">#000000</item>
    <item name="android:colorPrimaryDark">#000000</item>
    <item name="android:windowDrawsSystemBarBackgrounds">true</item>
</style>
Run Code Online (Sandbox Code Playgroud)

在此先感谢您的任何帮助,您可以发送我的方式!我怀疑我的问题与Google Now的主屏幕如何具有透明状态栏有关,并且仅在导航抽屉期间对其进行着色,但任何相反的消息都会很棒!

jus*_*asm 17

使用您的设置,导航抽屉只是一个视图android:background="@color/WindowBackground",在状态栏下方绘制其背景(您已定义为)(通过您设置为透明的颜色StatusBarActivityTheme).系统仅查看Activity主题以设置状态栏颜色; 分配android:statusBarColor给孩子没有任何效果.

简单的解决方案是将Nav Drawer片段布局更改为您android:background想要的颜色.如果您希望图像下方的部分保持白色,则可以选择在抽屉布局中添加一个空视图android:background="@color/WindowBackground"或其他颜色.


如果您希望导航抽屉中的内容延伸到状态栏下方,则需要更多工作.它首先被偏移的原因是因为你将android:fitsSystemWindows属性设置为true,而后者又调用视图的默认fitSystemWindows().正如文档所解释的那样,此方法采用插入(即我们的情况下状态栏的高度)并将其应用为视图的填充(在您的情况下,这将成为RelativeLayoutNav Drawer片段的顶部填充).

绕过此填充的fitSystemWindows()一种方法是覆盖视图的方法.我引导您访问Google的开源IO Schedule应用程序 - 具体来说,他们使用了Nav抽屉中ScrimInsetsScrollView的根元素.此修改适用于您所选择的颜色的纱幕(通过自定义设置属性)和消耗的插图,即没有更多的填充!ScrollViewapp:insetForeground

ScrimInsetsScrollView可作为模型来编写自己版本的任何浏览后代,真的-看到近了一致ScrimInsetsFrameLayout.