前言

自从MD设计规范出来后,关于系统状态栏的适配越受到关注,因为MD在5.0以后把系统状态栏的颜色改为可由开发者配置的,而在5.0之前则无法指定状态栏的颜色,所以这篇就说说使用Toolbar对系统状态栏的适配策略

主流App的适配效果

手Q在这方面适配非常好,将标题栏和状态栏合为一起了,和iOS效果一模一样,如下:

4.4、5.0+

4.4以下版本

4.4以下版本则是系统默认的黑色状态栏,因为4.4以下没办法对状态栏进行改变和配置。

关于手Q的适配,对4.4以上所有版本都保留着一致的UI效果,即使在5.0以上,舍弃了MD风格的状态栏效果。

而微信的状态栏则是对5.0以上开始适配MD风格了,但是对5.0以下状态栏则没进行适配,状态栏还是系统默认的黑色,下面是5.0+系统上的效果:

使用Toolbar进行全版本适配

根据上面适配效果,我们可以这样适配:

4.4以下系统则使用系统默认的状态栏,在4.4系统上将状态栏和标题栏合为一起了,在5.0以上系统有两种选择:

1.继续使用4.4上的效果,和手Q一样

2.适配MD风格

如下是这两种适配的效果:

第一种:

4.4以下系统上

4.4系统上:

5.0+系统上

第二种:

4.4以下系统上:

4.4系统上:

5.0+系统上

具体实现

第一种

主要就是在style文件中对4.4以上的设置透明状态栏windowTranslucentStatus为true,或者也可以在代码中设置,定义一个BaseActivity:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            // Translucent status bar
            getWindow().setFlags(
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }

然后在布局文件中对Toolbar设为:

android:fitsSystemWindows="true"
android:minHeight="?attr/actionBarSize"

这个至关重要,一般可以把Toolbar的布局为一个layout文件,以便后续复用。

1、values:

    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
         <!-- Customize your theme here. -->
    </style>

    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>

2、values-v19:

    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="android:windowTranslucentStatus">true</item>
    </style>

3、values-v21:

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

4、layout布局:

ImageView是RecyclerView头部添加

   <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycle_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        android:fitsSystemWindows="true"
        android:minHeight="?attr/actionBarSize"
        android:theme="@style/AppTheme.AppBarOverlay"
        app:popupTheme="@style/AppTheme.PopupOverlay">

        <TextView
            android:id="@+id/title"
            style="@style/TextAppearance.AppCompat.Subhead"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20sp" />

        <ImageView
            android:id="@+id/img"
            android:layout_width="48dp"
            android:layout_height="?attr/actionBarSize"
            android:layout_gravity="right"
            android:clickable="true"
            android:scaleType="centerInside"
            android:src="@android:drawable/stat_sys_headset" />
    </android.support.v7.widget.Toolbar>

</android.support.design.widget.CoordinatorLayout>

第二种

大致和第一种一样,差别就是values-v21的内容了

1、values:

    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
         <!-- Customize your theme here. -->
    </style>

    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>

2、values-v19:

    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="android:windowTranslucentStatus">true</item>
    </style>

3、values-v21:

    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="android:windowDrawsSystemBarBackgrounds">true</item>
        <item name="android:statusBarColor">@color/colorPrimaryDark</item>

4、layout布局:

和上面布局一样

实现真正的全屏显示,图片在状态栏下面

要实现图片可以显示在状态栏下面,如上图效果,则需要将状态栏设为透明的,而第二种在5.0以上并没有将状态栏设为透明,所以你如果使用第一种适配方案,则直接使用即可,如果使用第二种适配发难,那么需要在相应全屏的Activity中用代码将状态栏设为透明,因为values没有设置,然后将

AppTheme.NoActionBar设为该全屏Activity的主题,毕竟全屏,那么就ok了

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.sunzxy.demoapplication.TestActivity">

    <ImageView
        android:id="@+id/img"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"
        android:src="@mipmap/bg" />

</RelativeLayout>

效果:

或者不加图片,直接使用实现状态栏和Toolbar合为一体效果:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.sunzxy.demoapplication.TestActivity">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        android:fitsSystemWindows="true"
        android:minHeight="?attr/actionBarSize"
        android:theme="@style/AppTheme.AppBarOverlay"
        app:popupTheme="@style/AppTheme.PopupOverlay">

        <TextView
            android:id="@+id/title"
            style="@style/TextAppearance.AppCompat.Subhead"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left"
            android:textSize="20sp" />
    </android.support.v7.widget.Toolbar>
</RelativeLayout>

Android最佳实践之SystemBar状态栏全版本适配方案的更多相关文章

  1. Atitit.嵌入式web 服务器 java android最佳实践

    Atitit.嵌入式web 服务器 java android最佳实践 1. Android4.4.21 2. 自己的webserver1 3. CyberHTTP for Java  cybergar ...

  2. fir.im Weekly - 2016 年 Android 最佳实践列表

    2016 年已经过去一半,你在年初制定的成长计划都实现了吗? 学海无涯,技术成长不是一簇而就的事情.本期 fir.im Weekly 推荐 王下邀月熊_Chevalier的 我的编程之路--知识管理与 ...

  3. 听说你还不会用Dagger2?Dagger2 For Android最佳实践教程

    前言 Dagger2是现在非常火的一个依赖注入框架,目前由Google维护,在Github上面已经有12K star了.Dagger2的入门门槛其实是比较高的,据了解,目前有很多Android工程师对 ...

  4. Android 目前最稳定和高效的UI适配方案

    Android系统发布十多年以来,关于Android的UI的适配一直是开发环节中最重要的问题,但是我看到还是有很多小伙伴对Android适配方案不了解.刚好,近期准备对糗事百科Android客户端设计 ...

  5. Android最佳实践指南

    Updated on 2016/1/6 修正了一些翻译段落欢迎转载,但请保留译者链接:http://www.jianshu.com/p/613d28a3c8a0 Lessons learned fro ...

  6. 我的Android最佳实践之—— 常用的Intent.Action(转)

    1.从google搜索内容 Intent intent = new Intent(); intent.setAction(Intent.ACTION_WEB_SEARCH); intent.putEx ...

  7. android最佳实践之设备兼容性

    由于不同手机的尺寸大小,屏幕分辨率可能存在差异.在开发应用的时候,你或许遇到过这些的问题: 1, 为什么图片在另外的手机上显示的时候变小了,又或是缩小了? 2, 为什么在layout中定义好的格局在另 ...

  8. android最佳实践的建议(翻译自android-best-practices)

    Best practices in Android development Use Gradle and its recommended project structure 使用Gradle和其推荐的 ...

  9. Android最佳实践之Material Design

    Material概述及主题 学习地址:http://developer.android.com/training/material/get-started.html 使用material design ...

随机推荐

  1. 使用JAXB解析xml文件(二)

    前面一章简单演示了JAXB的用法,这个章节主要梳理一下JAXB常见的几个注解 1.@XmlRootElement 用于类级别的注解,对应xml的跟元素,常与 @XmlType 和 @XmlAccess ...

  2. Python中高阶函数讲解

    高阶函数讲解 1. 常规高阶函数 递归函数 格式:def func_name(variable): '''__doc__'''#函数的说明文档 if 条件表达式:#限制递归退出值 pass retur ...

  3. SQL之排序

    1.按多个列排序 经常需要按不止一个列进行数据排序.例如,如果要显示雇员名单,可能希望按姓和名排序(首先按姓排序,然后在每个姓中再按名排序).如果多个雇员有相同的姓,这样做很有用. 要按多个列排序,简 ...

  4. ThreadLocal基本原理及运用

    ThreadLocal提供本地线程变量.这个变量里面的值(通过get方法获取)是和其他线程分割开来的,变量的值只有当前线程能访问到,不像一般的类型比如Person,Student类型的变量,只要访问到 ...

  5. 借助Bodymovin播放svg动画

    svg动画,截取工具有点不忍直视了~~~ 为了实现上面的svg动画,可以使用bodymovin插件,简单配置之后,就可以直接可以实现在 AE(可视化操作,不用码代码)上面导出 svg的json数据,在 ...

  6. android 欢迎界面的制作

    再打开手机app的时候,最先映入我们眼帘的是一个覆盖手机全屏的欢迎界面,在这个界面显示出来的时候整个手机屏幕只会显示这一个界面,上面的标题栏,以及手机最顶端的状态栏都会消失,只有欢迎页面结束跳转到其他 ...

  7. vim 基本命令入门

    简介 vim是Linux 系统下类似于Windows的记事本的编辑器. vim 中经常使用的三种模式 一般模式:浏览文件内容. 插入模式:编辑文件内容. 底行模式:进行保存编辑内容,退出等操作. 基本 ...

  8. Linux shell爬虫实现树洞网自动回复Robot

    奇怪的赞数 人生在世,不如意事十之八九,可与言者无二三人.幸好我们生在互联网时代,现实中找不到可以倾诉的人还可以在网络上寻找发情绪宣泄口,树洞这类产品就是提供一个让人在网络上匿名倾诉的平台. 我是偶然 ...

  9. CMCC验证绕过POC

    大学的时候无意间发现绕过CMCC验证的方法(贫穷使人进步...),写了段POC脚本,时过两年,漏洞应该已经失效了(我猜 --),刚刚发现有人私信问我要,都那么久了鬼还记得写的什么啊,但确实看到了又不能 ...

  10. AC的故事大结局山寨版(下)(最大流)

    福建工程学院第十二届ACM程序设计大赛真题 AC的故事大结局山寨版(下) TimeLimit:2000MS  MemoryLimit:128MB 64-bit integer IO format:%l ...