Android最佳实践之SystemBar状态栏全版本适配方案
前言
自从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状态栏全版本适配方案的更多相关文章
- Atitit.嵌入式web 服务器 java android最佳实践
Atitit.嵌入式web 服务器 java android最佳实践 1. Android4.4.21 2. 自己的webserver1 3. CyberHTTP for Java cybergar ...
- fir.im Weekly - 2016 年 Android 最佳实践列表
2016 年已经过去一半,你在年初制定的成长计划都实现了吗? 学海无涯,技术成长不是一簇而就的事情.本期 fir.im Weekly 推荐 王下邀月熊_Chevalier的 我的编程之路--知识管理与 ...
- 听说你还不会用Dagger2?Dagger2 For Android最佳实践教程
前言 Dagger2是现在非常火的一个依赖注入框架,目前由Google维护,在Github上面已经有12K star了.Dagger2的入门门槛其实是比较高的,据了解,目前有很多Android工程师对 ...
- Android 目前最稳定和高效的UI适配方案
Android系统发布十多年以来,关于Android的UI的适配一直是开发环节中最重要的问题,但是我看到还是有很多小伙伴对Android适配方案不了解.刚好,近期准备对糗事百科Android客户端设计 ...
- Android最佳实践指南
Updated on 2016/1/6 修正了一些翻译段落欢迎转载,但请保留译者链接:http://www.jianshu.com/p/613d28a3c8a0 Lessons learned fro ...
- 我的Android最佳实践之—— 常用的Intent.Action(转)
1.从google搜索内容 Intent intent = new Intent(); intent.setAction(Intent.ACTION_WEB_SEARCH); intent.putEx ...
- android最佳实践之设备兼容性
由于不同手机的尺寸大小,屏幕分辨率可能存在差异.在开发应用的时候,你或许遇到过这些的问题: 1, 为什么图片在另外的手机上显示的时候变小了,又或是缩小了? 2, 为什么在layout中定义好的格局在另 ...
- android最佳实践的建议(翻译自android-best-practices)
Best practices in Android development Use Gradle and its recommended project structure 使用Gradle和其推荐的 ...
- Android最佳实践之Material Design
Material概述及主题 学习地址:http://developer.android.com/training/material/get-started.html 使用material design ...
随机推荐
- 8.QT-对话框(模态与非模态)
对话框介绍 对话框是于用户进行简易交互的顶层窗口 QDialog是Qt中所有对话框窗口的父类,是一种容器类型的组件 QDialog继承于QWidget类,如下图所示: QWidget和QDialog有 ...
- mac下怎么删除隐藏文件比如 .Trashes文件
U盘和移动硬盘接入Mac时会产生.Trashes,.Spotlight-V100,.fseventsd等文件 每插入Mac一次,都会检查是否有这些文件,如果没有,就会创建这些文件 特别是有时候,在文件 ...
- easyui常见问题
EasyUi combobox 下拉列表JS添加首/尾选择项 下拉列表获取数据后,再动态添加一项数据项,如:"<option value=''>全部</option> ...
- selenium常用内容
一.声明浏览器对象 注意点一,Python文件名或者包名不要命名为selenium,会导致无法导入 from selenium import webdriver #webdriver可以认为是浏览器的 ...
- day04 Java Web 开发入门
day04 Java Web 开发入门 1. web 开发相关介绍 2. web 服务器 3. Tomcat服务器启动的问题 4. Tomcat目录结构 5. Web应用程序(虚拟目录映射,缺省web ...
- vue-cli搭建项目的目录结构及说明
vue-cli基于webpack搭建项目的目录结构 build文件夹 ├── build // 项目构建的(webpack)相关代码 │ ├── build.js ...
- C语言关闭日志文件时忘了将日志文件全局变量指针置为NULL
C语言写了一个write_log函数以写日志,写了一个close_log_file函数以关闭日志,声明了一个日志文件全局变量文件指针plogFile. write_log中首先判断plogFile是否 ...
- 用go实现常用算法与数据结构——队列(queue)
queue 简介 队列是一种非常常见的数据结构,日常生活中也能经常看到.一个典型的队列如下图(图片来自 segmentfault): 可以看出队列和我们日常生活中排队是基本一致的.都遵循 FIFO(F ...
- Docker 网络
Docker 的网络实现其实就是利用了 Linux 上的网络名字空间和虚拟网络设备(特别是 veth pair).建议先熟悉了解这两部分的基本概念再阅读本章. 基本原理 首先,要实现网络通信,机器需要 ...
- cassandra 3.x官方文档(5)---探测器
写在前面 cassandra3.x官方文档的非官方翻译.翻译内容水平全依赖本人英文水平和对cassandra的理解.所以强烈建议阅读英文版cassandra 3.x 官方文档.此文档一半是翻译,一半是 ...