使导航抽屉后面绘制的状态栏 [英] Make navigation drawer draw behind status bar

查看:158
本文介绍了使导航抽屉后面绘制的状态栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个导航抽屉一样从材料规范(如来自新的Gmail应用程序的)的人。需要注意的资产净值抽屉里的内容如何绘制状态栏后面:

使用克里斯·巴内斯的回答从<一个href="http://stackoverflow.com/questions/26440879/how-do-i-use-drawerlayout-to-display-over-the-actionbar-toolbar-and-under-the-st">this问题,我是能够成功地使导航抽屉里我的状态栏后面的应用程序平局;该工作的罚款。什么是不工作是绘制状态栏后面的导航抽屉的内容。我想在我的抽屉中的蓝色图像被状态栏后面显示,但是该区域被绘制的状态栏的颜色,如在此屏幕截图所示。

所以,我怎样才能让我的抽屉式导航栏绘制状态栏后面的区域?我下面贴我的项目的相关部分。

包含导航抽屉基地布局:

 &LT; android.support.v4.widget.DrawerLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:ID =@ + ID / nav_drawer_layout
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:方向=垂直
    机器人:fitsSystemWindows =真正的&GT;

    &LT;! - 的FrameLayout显示片段 - &GT;
    &LT;的FrameLayout
        机器人:ID =@ + ID /内容
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent
        机器人:layout_above =@ + ID / warning_container/&GT;

    &LT;的FrameLayout
        机器人:ID =@ + ID / navigation_drawer_fragment_container
        机器人:layout_width =300dp
        机器人:layout_height =match_parent
        机器人:fitsSystemWindows =真
        机器人:layout_gravity =开始&GT;

        &LT;片段
            机器人:ID =@ + ID / navigation_drawer_fragment
            机器人:名称=com.thebluealliance.androidclient.fragments.NavigationDrawerFragment
            机器人:layout_width =match_parent
            机器人:layout_height =match_parent
            工具:布局=@布局/ fragment_navigation_drawer/&GT;

    &LT; /的FrameLayout&GT;

&LT; /android.support.v4.widget.DrawerLayout>
 

我的活动主题

 &LT;样式名称=AppThemeNoActionBar父=AppTheme&GT;
    &LT;项目名称=windowActionBar&GT;假&LT; /项目&GT;
    &LT;项目名称=机器人:windowNoTitle&GT;真&LT; /项目&GT;
    &LT;项目名称=机器人:windowDrawsSystemBarBackgrounds&GT;真&LT; /项目&GT;
    &LT;项目名称=机器人:statusBarColor&GT; @android:彩色/透明&LT; /项目&GT;
&LT; /风格&GT;
 

的onCreate()我的活动,我做到以下几点:

  mDrawerLayout.setStatusBarBackground(R.color.primary_dark);
 

解决方案

我发现这样做在Android 5.0的最佳方式。关键是使用 ScrimInsetFrameLayout 作为导航抽屉的根元素(在第二视图 DrawerLayout )。这将使内容扩展以填充状态栏后面的空间。要正确颜色的插图,你可以设置以下属性 ScrimInsetFrameLayout

 应用程序:insetForeground =#4000
 

此外,请确保您有安卓!fitsSystemWindows =真正的上的网眼织物布局

该人士$ ​​C $ C为 ScrimInsetFrameLayout 可以在这里找到:<一href="https://github.com/google/iosched/blob/master/android/src/main/java/com/google/samples/apps/iosched/ui/widget/ScrimInsetsFrameLayout.java">https://github.com/google/iosched/blob/master/android/src/main/java/com/google/samples/apps/iosched/ui/widget/ScrimInsetsFrameLayout.java

I'm trying to create a Nav Drawer like the one from the Material spec (like the one from the new gmail app). Note how the contents of the nav drawer draw behind the status bar:

Using Chris Banes' answer from this question, I was able to successfully make the navigation drawer in my app draw behind the status bar; that's working fine. What isn't working is drawing the contents of the nav drawer behind the status bar. I want the blue image in my drawer to be displayed behind the status bar, but that area is drawn with the color of status bar, as seen in this screenshot.

So, how can I make my navigation drawer draw in the area behind the status bar? I've posted the relevant parts of my project below.

Base layout containing the navigation 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/nav_drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:fitsSystemWindows="true">

    <!-- Framelayout to display Fragments -->
    <FrameLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/warning_container" />

    <FrameLayout
        android:id="@+id/navigation_drawer_fragment_container"
        android:layout_width="300dp"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        android:layout_gravity="start">

        <fragment
            android:id="@+id/navigation_drawer_fragment"
            android:name="com.thebluealliance.androidclient.fragments.NavigationDrawerFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:layout="@layout/fragment_navigation_drawer" />

    </FrameLayout>

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

Theme of my activity

<style name="AppThemeNoActionBar" parent="AppTheme">
    <item name="windowActionBar">false</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowDrawsSystemBarBackgrounds">true</item>
    <item name="android:statusBarColor">@android:color/transparent</item>
</style>

In onCreate() of my activity, I do the following:

mDrawerLayout.setStatusBarBackground(R.color.primary_dark);

解决方案

I found the best way to do this on Android 5.0. The key is to use a ScrimInsetFrameLayout as the root element of the navigation drawer (the second View in the DrawerLayout). This will make the content expand to fill the space behind the status bar. To color the inset properly, you can set the following attribute on the ScrimInsetFrameLayout:

app:insetForeground="#4000"

Also, make sure that you have android:fitsSystemWindows="true" on the scrim layout!

The source code for the ScrimInsetFrameLayout can be found here: https://github.com/google/iosched/blob/master/android/src/main/java/com/google/samples/apps/iosched/ui/widget/ScrimInsetsFrameLayout.java

这篇关于使导航抽屉后面绘制的状态栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆