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 ...
随机推荐
- 用solidity语言开发代币智能合约
智能合约开发是以太坊编程的核心之一,而代币是区块链应用的关键环节,下面我们来用solidity语言开发一个代币合约的实例,希望对大家有帮助. 以太坊的应用被称为去中心化应用(DApp),DApp的开发 ...
- Android开发Java基础之Java语言基础(1)
Java中的基本数据类型 整数类型 整数类型用来存储整数数值,既没有小数部分的数值.可以是正数,也可以是负数.整数类型在Java程序中有三种表现形式,分别是十进制,八进制,十六进制. 整型数据根据它所 ...
- Xcode8 添加PCH文件
转自默默desire 1.) 打开你的Xcode工程. 在Supporting Files目录下,选择 File > New > File > iOS > Other > ...
- Python小代码_3_购物车
product_list = [ ('MacBook', 9000), ('kindle', 500), ('tesla', 900000), ('book', 100), ('bike', 2000 ...
- vue--"卡片层叠" 组件 开发小记
背景:影城移动点餐web App增加会员卡支付功能 需求:确认订单页点击会员卡项弹出会员卡列表,多张会员卡依次叠加覆盖上一张80%的高度,点击任意卡片则改卡片置为当前卡片,只有当前卡片显示全部卡片信息 ...
- 模仿天猫实战【SSM版】——后台开发
上一篇文章链接:模仿天猫实战[SSM版]--项目起步 后台需求分析 在开始码代码之前,还是需要先清楚自己要做什么事情,后台具体需要实现哪些功能: 注意: 订单.用户.订单.推荐链接均不提供增删的功能. ...
- ctf writeup之程序员密码
起因 在v2ex上看到有人发了一篇帖子,说做了一个程序员小游戏,遂试玩了一下. 游戏的地址在这里: http://www.bettertomissthantomeet.com/pages/level. ...
- Docker常见仓库MongoDB
MongoDB 基本信息 MongoDB 是开源的 NoSQL 数据库实现. 该仓库提供了 MongoDB 2.2 ~ 2.7 各个版本的镜像. 使用方法 默认会在 27017 端口启动数据库. $ ...
- 为什么《Dive into Python》不值得推荐
2010 年 5 月 5 日更新:我翻译了一篇<<Dive Into Python>非死不可>作为对本文观点的进一步支持和对评论的回复,请见:http://blog.csdn. ...
- HOG OpenCV 代码片段
直接上代码: #include <opencv2/opencv.hpp> using namespace cv; #include <cmath> using namespac ...