转载请标明出处:

http://blog.csdn.net/lmj623565791/article/details/48649563

本文出自:【张鸿洋的博客】

一、概述

近期注意到QQ新版使用了沉浸式状态栏,ok,先声明一下:本篇博客效果下图:

关于这个状态栏变色到底叫「Immersive Mode」/「Translucent Bars」有兴趣可以去 为什么在国内会有很多用户把 「透明栏」(Translucent Bars)称作 「沉浸式顶栏」?上面了解了解,请勿指点我说的博文标题起得不对,thx。

恩,接下来正题。

首先只有大于等于4.4版本支持这个半透明状态栏的效果,但是4.4和5.0的显示效果有一定的差异,所有本篇博文内容为:

  • 如何实现半透明状态栏效果在大于4.4版本之上。
  • 如何让4.4的效果与5.0的效果尽可能一致。

看了不少参考文章,都介绍到这个库,大家可以了解:SystemBarTint

不过本篇博文并未基于此库,自己想了个hack,对于此库源码有空再看了。


二、效果图

先贴下效果图,以便和实现过程中做下对比

  • 4.4 模拟器

  • 5.x 真机

[new]贴个如果顶部是图片的效果图,其实是一样的,为了方便我就放侧栏的顶部了。

稍等,csdn图片服务器异常…

ok,有了效果图之后就开始看实现了。


三、实现半透明状态栏

因为本例使用了NavigationView,所以布局代码稍多,当然如果你不需要,可以自己进行筛减。

注意引入相关依赖:

  1. compile 'com.android.support:appcompat-v7:22.2.1'
  2. compile 'com.android.support:support-v4:22.2.1'
  3. compile 'com.android.support:design:22.2.0'

(一)colors.xml 和 styles.xml

首先我们定义几个颜色:

res/values/color.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <color name="primary">#FF03A9F4</color>
  4. <color name="primary_dark">#FF0288D1</color>
  5. <color name="status_bar_color">@color/primary_dark</color>
  6. </resources>

下面定义几个styles.xml

注意文件夹的路径:

values/styles.xml

  1. <resources>
  2. <style name="BaseAppTheme" parent="Theme.AppCompat.Light.NoActionBar">
  3. <!-- Customize your theme here. -->
  4. <item name="colorPrimary">@color/primary</item>
  5. <item name="colorPrimaryDark">@color/primary_dark</item>
  6. <item name="colorAccent">#FF4081</item>
  7. </style>
  8. <!-- Base application theme. -->
  9. <style name="AppTheme" parent="@style/BaseAppTheme">
  10. </style>
  11. </resources>

values-v19

  1. <resources>
  2. <style name="AppTheme" parent="@style/BaseAppTheme">
  3. <item name="android:windowTranslucentStatus">true</item>
  4. </style>
  5. </resources>

ok,这个没撒说的。注意我们的主题是基于NoActionBar的,android:windowTranslucentStatus这个属性是v19开始引入的。

(二)布局文件

activity_main.xml

  1. <android.support.v4.widget.DrawerLayout
  2. xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. >
  8. <LinearLayout
  9. android:id="@+id/id_main_content"
  10. android:layout_width="match_parent"
  11. android:layout_height="match_parent"
  12. android:orientation="vertical">
  13. <android.support.v7.widget.Toolbar
  14. android:id="@+id/id_toolbar"
  15. android:layout_width="match_parent"
  16. android:layout_height="wrap_content"
  17. android:background="?attr/colorPrimary"
  18. android:fitsSystemWindows="true"
  19. app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
  20. <TextView
  21. android:id="@+id/id_tv_content"
  22. android:layout_width="match_parent"
  23. android:layout_height="0dp"
  24. android:layout_weight="1"
  25. android:gravity="center"
  26. android:text="HelloWorld"
  27. android:textSize="30sp"/>
  28. </LinearLayout>
  29. <android.support.design.widget.NavigationView
  30. android:id="@+id/id_nv_menu"
  31. android:layout_width="match_parent"
  32. android:layout_height="match_parent"
  33. android:layout_gravity="start"
  34. android:fitsSystemWindows="true"
  35. app:headerLayout="@layout/header_just_username"
  36. app:menu="@menu/menu_drawer"
  37. />
  38. </android.support.v4.widget.DrawerLayout>

DrawerLayout内部一个LinearLayout作为内容区域,一个NavigationView作为菜单。

注意下Toolbar的高度设置为wrap_content。

然后我们的NavigationView中又依赖一个布局文件和一个menu的文件。

header_just_username.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="192dp"
  5. android:background="?attr/colorPrimaryDark"
  6. android:orientation="vertical"
  7. android:padding="16dp"
  8. android:fitsSystemWindows="true"
  9. android:theme="@style/ThemeOverlay.AppCompat.Dark">
  10. <TextView
  11. android:id="@+id/id_link"
  12. android:layout_width="wrap_content"
  13. android:layout_height="wrap_content"
  14. android:layout_alignParentBottom="true"
  15. android:layout_marginBottom="16dp"
  16. android:text="http://blog.csdn.net/lmj623565791"/>
  17. <TextView
  18. android:id="@+id/id_username"
  19. android:layout_width="wrap_content"
  20. android:layout_height="wrap_content"
  21. android:layout_above="@id/id_link"
  22. android:text="Zhang Hongyang"/>
  23. <ImageView
  24. android:layout_width="72dp"
  25. android:layout_height="72dp"
  26. android:layout_above="@id/id_username"
  27. android:layout_marginBottom="16dp"
  28. android:src="@mipmap/ic_launcher"/>
  29. </RelativeLayout>

menu的文件就不贴了,更加详细的可以去参考Android 自己实现 NavigationView [Design Support Library(1)]

大体看完布局文件以后,有几个点要特别注意:

  • ToolBar高度设置为wrap_content
  • ToolBar添加属性android:fitsSystemWindows="true"
  • header_just_username.xml的跟布局RelativeLayout,添加属性android:fitsSystemWindows="true"

android:fitsSystemWindows这个属性,主要是通过调整当前设置这个属性的view的padding去为我们的status_bar留下空间。

根据上面的解释,如果你不写,那么状态栏和Toolbar就会有挤一块的感觉了,类似会这样:

ok,最后看下代码。

(三)Activity的代码

  1. package com.zhy.colorfulstatusbar;
  2. import android.os.Bundle;
  3. import android.support.v7.app.AppCompatActivity;
  4. import android.support.v7.widget.Toolbar;
  5. public class MainActivity extends AppCompatActivity
  6. {
  7. @Override
  8. protected void onCreate(Bundle savedInstanceState)
  9. {
  10. super.onCreate(savedInstanceState);
  11. setContentView(R.layout.activity_main);
  12. Toolbar toolbar = (Toolbar) findViewById(R.id.id_toolbar);
  13. setSupportActionBar(toolbar);
  14. //StatusBarCompat.compat(this, getResources().getColor(R.color.status_bar_color));
  15. //StatusBarCompat.compat(this);
  16. }
  17. }

没撒说的,就是setSupportActionBar。

那么现在4.4的效果图是:

其实还不错,有个渐变的效果。

现在5.x的效果:

可以看到5.x默认并非是一个渐变的效果,类似是一个深一点的颜色。

在看看我们md的规范

状态栏应该是一个比Toolbar背景色,稍微深一点的颜色。

这么看来,我们还是有必要去为4.4做点适配工作,让其竟可能和5.x显示效果一致,或者说尽可能符合md的规范。


四、调整4.4的显示方案

那么问题来了?如何做呢?

咱们这么看,4.4之后加入windowTranslucentStatus的属性之后,也就是我们可以用到状态栏的区域了。

既然我们可以用到这块区域,那么我们只要在根布局去设置一个与状态栏等高的View,设置背景色为我们期望的颜色就可以了。

于是有了以下的代码:

  1. package com.zhy.colorfulstatusbar;
  2. import android.annotation.TargetApi;
  3. import android.app.Activity;
  4. import android.content.Context;
  5. import android.graphics.Color;
  6. import android.os.Build;
  7. import android.view.View;
  8. import android.view.ViewGroup;
  9. /**
  10. * Created by zhy on 15/9/21.
  11. */
  12. public class StatusBarCompat
  13. {
  14. private static final int INVALID_VAL = -1;
  15. private static final int COLOR_DEFAULT = Color.parseColor("#20000000");
  16. @TargetApi(Build.VERSION_CODES.LOLLIPOP)
  17. public static void compat(Activity activity, int statusColor)
  18. {
  19. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
  20. {
  21. if (statusColor != INVALID_VAL)
  22. {
  23. activity.getWindow().setStatusBarColor(statusColor);
  24. }
  25. return;
  26. }
  27. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP)
  28. {
  29. int color = COLOR_DEFAULT;
  30. ViewGroup contentView = (ViewGroup) activity.findViewById(android.R.id.content);
  31. if (statusColor != INVALID_VAL)
  32. {
  33. color = statusColor;
  34. }
  35. View statusBarView = new View(activity);
  36. ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
  37. getStatusBarHeight(activity));
  38. statusBarView.setBackgroundColor(color);
  39. contentView.addView(statusBarView, lp);
  40. }
  41. }
  42. public static void compat(Activity activity)
  43. {
  44. compat(activity, INVALID_VAL);
  45. }
  46. public static int getStatusBarHeight(Context context)
  47. {
  48. int result = 0;
  49. int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
  50. if (resourceId > 0)
  51. {
  52. result = context.getResources().getDimensionPixelSize(resourceId);
  53. }
  54. return result;
  55. }
  56. }

代码的思路很简单,根据Activity找到android.R.content,在其中添加一个View(高度为statusbarHeight,背景色为我们设置的颜色,默认为半透明的黑色)。

那么只需要在Activity里面去写上:

  1. StatusBarCompat.compat(this);

就可以了。

如果你希望自己设置状态看颜色,那么就用这个方法:

  1. StatusBarCompat.compat(this, getResources().getColor(R.color.status_bar_color));

这样的话我们就解决了4.4到5.x的适配问题,一行代码解决,感觉还是不错的。

最后提一下,对于5.0由于提供了setStatusBarColor去设置状态栏颜色,但是这个方法不能在主题中设置windowTranslucentStatus属性。所以,可以编写一个value-v21文件夹,里面styles.xml写入:

  1. <resources>
  2. <!-- Base application theme. -->
  3. <style name="AppTheme" parent="@style/BaseAppTheme">
  4. </style>
  5. </resources>

其实就是不要有windowTranslucentStatus属性。

接下来,对于默认的效果就不测试了,参考上面的效果图。

我们测试个设置状态栏颜色的,我们这里设置个红色。

  • 4.4 模拟器

  • 5.x 真机

ok,这样就结束啦~~

源码地址:https://github.com/hongyangAndroid/ColorfulStatusBar


欢迎关注我的微博:

http://weibo.com/u/3165018720


群号:463081660,欢迎入群

微信公众号:hongyangAndroid

(欢迎关注,第一时间推送博文信息)

参考

版权声明:本文为博主原创文章,未经博主允许不得转载。

Android 沉浸式状态栏攻略 让你的状态栏变色吧的更多相关文章

  1. Android 沉浸式状态栏 实现方式二 ( 更简单 )

    以前写过一个沉浸式状态栏 的实现方式 Android 沉浸式状态栏 实现方式一 现在有个更为简单的实现方式 . 相关链接 http://www.apkbus.com/forum.php?mod=vie ...

  2. Android沉浸式(侵入式)标题栏(状态栏)Status(三)

     Android沉浸式(侵入式)标题栏(状态栏)Status(三) 从附录文章1,2可以看到,依靠Android系统提供的标准方案,状态栏即便在透明状态下,仍然有些半透明而不是全透明.本文是And ...

  3. Android沉浸式(侵入式)标题栏(状态栏)Status(二)

     Android沉浸式(侵入式)标题栏(状态栏)Status(二) 附录1以xml写style实现了Android沉浸式(侵入式)状态栏(标题栏),同样以上层Java代码实现.在附录文章1的基础上 ...

  4. Android沉浸式(侵入式)标题栏(状态栏)Status(一)

     Android沉浸式(侵入式)标题栏(状态栏)Status(一) 现在越来越多的APP设计采用这种称之为沉浸式状态栏(Status)的设计,这种沉浸式状态栏又称之"侵入式"状 ...

  5. android 沉浸式状态栏的实现

    本文介绍一种简单的实现沉浸式状态栏的方法,要高于或等于api19才可以. 实现android沉浸式状态栏很简单,添加代码两步就可以搞定. 一.在activity中添加 getWindow().addF ...

  6. [置顶] Xamarin android沉浸式状态栏

    虽然关于android "沉浸式"状态栏有很多博客介绍过,从小菜到大神无一例外.我第一次看到这种"沉浸"式的效果我也以为真的是这么叫,然而根本不是这么回事,完全 ...

  7. Android 沉浸式状态栏完美解决方案

    现在搜索Android 沉浸式状态栏,真的是一堆一堆,写的特别多,但是真正用的舒服的真没有,在这里自己整理一下开发记录 注意,在使用这个步骤过程之前,请把之前设置的代码注释一下 把布局带有androi ...

  8. Android沉浸式状态栏(透明状态栏)最佳实现

    Android沉浸式状态栏(透明状态栏)最佳实现 在Android4.4之前,我们的应用没法改变手机的状态栏颜色,当我们打开应用时,会出现上图中左侧的画面,在屏幕的顶部有一条黑色的状态栏,和应用的风格 ...

  9. 【Android实战】Android沉浸式状态栏实现(下)

    之前的Android沉浸式状态栏实现并没有考虑软键盘的影响,接下来的内容将会针对这个问题给出解决方式,先看一下效果图 这个是一个留言板的效果图: 即弹出软键盘的时候并不会导致整个布局上移. 详细怎样实 ...

随机推荐

  1. ubuntu安装qq教程

    安装策略是wine+wine QQ TM2013,wine QQ TM2013是一款专门为wine进行优化的版本 我的ubuntu系统是14.04版本,64位 1. sudo apt-get inst ...

  2. ruby中printf "%x"%-4为何会打印开头..

    先看一下ruby中printf "%x" % -4的返回结果: irb(main):134:0> printf "%x\n" % -4 ..fc 前面的. ...

  3. 使用kubeadm搭建Kubernetes(1.10.2)集群(国内环境)

    目录 目标 准备 主机 软件 步骤 (1/4)安装 kubeadm, kubelet and kubectl (2/4)初始化master节点 (3/4) 安装网络插件 (4/4)加入其他节点 (可选 ...

  4. 我的.net并发系列文章及项目经验整理

    一直在关注研究.net下的并发处理,之前也发布过几篇文章,今天就都整理下. 使用BlockingCollection来做并发处理,同时增加并发队列来做并发处理时的退出判断: 你真的知道.NET Fra ...

  5. jQuery live()方法使用及变更(事件委托)

    根据jQuery的官方描述,live方法在1.7中已经不建议使用,在1.9中删除了这个方法.并建议在以后的代码中使用on方法来替代. on方法可以接受三个参数:事件名.触发选择器.事件函数. 需要特别 ...

  6. 多台或者集群环境下如何保证spring定时器只执行一个

    先说一下我们的系统, 在65和66上分别部署有weblogic节点,共计四个,在项目中我们的定时器会隔一段时间就从其它的五个系统中取数据,这时就出现了问题,本来取一次数据就可以的,现在重复执行了三次, ...

  7. Xamarin引用第三方包错误解决方法

    http://www.cnblogs.com/ThenDog/p/7623720.html

  8. jquery中利用队列依次执行动画

    如果有5个隐藏的div,要让它们依次显示,通常的做法是要一个一个嵌套在回调函数里面,这样导致代码看起来非常不直观. $("#div1").slideDown(1000,functi ...

  9. 10.API 接口自动化测试的基本原理

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 28.0px Helvetica } p.p2 { margin: 0.0px 0.0px 0.0px 0. ...

  10. vue中get和post请求

    import axios from 'axios'; import router from '@/router'; import {     setSessionStorage,     getSes ...