注意:下面效果实现方法仅仅在Android Lollipop下完毕,并如果你使用Theme.AppCompat.NoActionBar或者是Theme.AppCompat.Light.NoActionBar主题

Material Design有个非常酷炫的效果,就是DrawerLayout隐藏的那一部分在拉开的时候,本应该被ActionBar和status
bar挡住的那部分,如今能够露出来了。

这样的效果已有人给出效果的实现方法了,可是对于非常多人来说还是不知道它是怎样实现的,所以这个这里先讲讲原理:

在新的框架和支持包下,我们可以实现下面功能:首先使用Toolbar来取代ActionBar,这样我们就行把ActionBar嵌入到我们的View体系中。然后我们"禁用"系统的status
bar,由DrawerLayout来处理status bar,最后抽屉部分往上移,或者裁剪掉status
bar那一部分。

控制status bar

在你的values-v21里面加入新的主题,并设置一下属性:

values-v21/themes.xml

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

这里解释一下:

windowDrawsSystemBarBackgrounds,将它设置为true,系统将在你的window里面绘制status
bar。默觉得TRUE。之所以要写出来是由于你的theme有可能是继承过来的,确保为true。(在这里小插曲一下,因调试时。总以为凝视了这段代码就以为是false。程序猿思维害苦了我。另外从命名来看。Android把它称为system bar,可能是为了与能被我们处理的status bar区分开而做的改变。

statusBarColor设置为透明是由于我们不再须要系统的status bar,由于我们无法控制它的位置,后面我们将交由DrawerLayout来处理。

使用DrawerLayout

首先,你的布局文件应该是和这个类似的:

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/my_drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"> <!-- Your normal content view -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <!-- We use a Toolbar so that our drawer can be displayed
in front of the action bar -->
<android.support.v7.widget.Toolbar
android:id="@+id/my_awesome_toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary" /> <!-- The rest of your content view --> </LinearLayout> <!-- The navigation drawer -->
<ScrimInsetsFrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="304dp"
android:layout_height="match_parent"
android:layout_gravity="left"
android:background="@android:color/white"
android:elevation="10dp"
android:fitsSystemWindows="true"
app:insetForeground="#4000"> <!-- Your drawer content --> </ScrimInsetsFrameLayout> </android.support.v4.widget.DrawerLayout>

在这里布局里面我们用到了一个的开源类ScrimInsetsFrameLayout,它的主要作用就是利用fitsSystemWindows的回调方法fitSystemWindows(Rect
insets)
来获取status bar的大小,然后调整画布以达到去掉status bar的效果。所以我们须要在ScrimInsetsFrameLayout下设置fitsSystemWindows为true。当然你也能够不使用这个类,而改用layout_marginTop属性来达到效果。

insetForeground这个属性是ScrimInsetsFrameLayout自带的,表示插入区域的前景色,我们设置为带透明的黑色#4000。别忘了使用这个属性须要加入例如以下代码到attrs.xml里:

values/attrs.xml

<declare-styleable name="ScrimInsetsView">
<attr name="insetForeground" format="reference|color" />
</declare-styleable>

自此,我们已经实现了将DrawerLayout抽屉的那一部分显示在Toolbar和system
bar(为了和以下的status bar区分,我们称为system bar)之间了,但是system bar的颜色被我们设置了透明,所以我们接下来要改变status bar的颜色。

改变status bar的颜色

你可能已经注意到刚才的布局里面DrawerLayoutfitsSystemWindows属性设置了为true,这是由于我们要在代码里面使用了DrawerLayout设置status
bar颜色的方法:

// 在这里我们获取了主题暗色,并设置了status bar的颜色
TypedValue typedValue = new TypedValue();
getTheme().resolveAttribute(R.attr.colorPrimaryDark, typedValue, true);
int color = typedValue.data; // 注意setStatusBarBackgroundColor方法须要你将fitsSystemWindows设置为true才会生效
DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.my_drawer_layout);
drawerLayout.setStatusBarBackgroundColor(color);

有关获取attr属性值的方法。能够看我另外一篇文章。点这里

使用Toolbar来取代ActionBar

在代码里面这样设置:

Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
setSupportActionBar(toolbar);

最后

赶紧看看你出来的效果是不是和上面的一样?为了实现这一点效果并理解它,真要认真琢磨一下,最后感谢你的阅读到这里。

怎样将DrawerLayout显示在ActionBar/Toolbar和status bar之间的更多相关文章

  1. 解决ActionBar中的item不显示在ActionBar的问题

    今天在用ActionBar,需要增加一个菜单选项,按教程在/res/menu下对应的布局文件中添加了一个item,但是它却是显示在overflow中,而不是直接显示在ActionBar当中的.我的布局 ...

  2. 在RecyclerView列表滚动的时候显示或者隐藏Toolbar

    先看一下效果: 本文将讲解如何实现类似于Google+应用中,当列表滚动的时候,ToolBar(以及悬浮操作按钮)的显示与隐藏(向下滚动隐藏,向上滚动显示),这种效果在Material Design ...

  3. WPF 4 开发Windows 7 任务栏(Overlay Icon、Thumbnail Toolbar、Progress Bar)

    原文:WPF 4 开发Windows 7 任务栏(Overlay Icon.Thumbnail Toolbar.Progress Bar)      在上一篇我们介绍了如何在WPF 4 中开发Wind ...

  4. Android下拉上滑显示与隐藏Toolbar另一种实现

    public abstract class RecyclerViewScrollListener extends RecyclerView.OnScrollListener { private sta ...

  5. colorAccent、colorPrimary、colorPrimaryDark actionbar toolbar navigationbar

    伴随着Android5.0的发布也更新了support-v7-appcompat 到V21,其中增加了ToolBar.recyclerview.cardview等控件. Android5.0对改变AP ...

  6. Android colorAccent、colorPrimary、colorPrimaryDark actionbar toolbar navigationbar

    伴随着Android5.0的发布也更新了support-v7-appcompat 到V21,其中增加了ToolBar.recyclerview.cardview等控件. Android5.0对改变AP ...

  7. Android UI ActionBar功能-自定义 Action Bar 样式

    ActionBar的样式官方提供了三种: Theme.Holo Theme.Holo.Light Theme.Holo.Light.DarkActionBar 但不仅仅是这三种,我们还可以自己定义Ac ...

  8. vim——打开多个文件、同时显示多个文件、在文件之间切换

    打开多个文件: 1.vim还没有启动的时候: 在终端里输入  vim file1 file2 ... filen便可以打开所有想要打开的文件 2.vim已经启动 输入 :open file 可以再打开 ...

  9. Android UI ActionBar功能-在 Action Bar 上添加按钮

    在ActionBar上添加按钮实现某些功能最常见的Application的功能如:在ActionBar上添加一个搜索按钮: 首先官方文档说明:http://wear.techbrood.com/tra ...

随机推荐

  1. 一篇文章告你python能做什么,该不该学?好不好学?适不适合学?

    一.python好学吗?简单吗?容易学吗?没有编程的领取能学吗? 最近有很多小伙伴都在问我这些问题.在这里,我想说,python非常简单易学. 1,简单, Python 非常易于读写,开发者可以把更多 ...

  2. 微信小程序(template的使用)

    小程序的template是一个模版功能,在创建一个template后,其他的页面可以引用,相比component较简单.方便! template只需要两个文件,一个wxss文件和wxml文件,也只有这 ...

  3. cc.AudioSource

    cc.AudioSource1:AudioSource组件是音频源组件, 发出声音的源头2: AudioSource组件面板: clip: 声源的播放的音频对象: AudioClip, mp3, wa ...

  4. Centos7 64bit Linux系统安装SVN 和 http访问配置

    第一步,更新源,并通过yum下载安装svn [root@virde ~]# yum update [root@virde ~]# yum -y install subversion 安装过程中,全部选 ...

  5. 虚拟机Linux与本地虚拟网卡配置---NAT链接方式

    虚拟机Linux与本地虚拟网卡配置---NAT链接方式 **********这是我亲自尝试多次实践出来的结果,不是复制粘贴************************* 首先进行初始化,这样避免有 ...

  6. 第二十二节:scrapy爬虫识别验证码(一)类库安装

    一.安装tesserocr 1.首先下载tesseract:https://digi.bib.uni-mannheim.de/tesseract/ ,我下载的是tesseract-ocr-setup- ...

  7. 缩小Oracle目录下UNDOTBS01.DBF文件的大小

    缩小Oracle目录下UNDOTBS01.DBF文件的大小 分类: Oracle 使用sys用户登录Oracle 方法一:重置表空间大小 执行ALTER DATABASE DATAFILE 'D:OR ...

  8. JavaEE JDBC ResultSet内外移动

    ResultSet内外移动 @author ixenos 内外移动指位置光标的移动 内移动就是一个ResultSet得到后的那个光标! 外移动就是多个ResultSet的迭代 内移动 一般的数据库都不 ...

  9. hdu 2433 Travel (最短路树)

     One day, Tom traveled to a country named BGM. BGM is a small country, but there are N (N <= 100) ...

  10. Sumdiv(poj1845)

    题意:求A^B的因子的和. /* 首先将A分解 A=p1^a1*p2^a2*...*pn*an A^B=p1^a1B*p2^a2B*...*pn*anB 因子之和sum=(1+p1+p1^2+...+ ...