怎么把android的状态栏改成蓝色,教程分享:如何实现Android沉浸式状态栏——教你让你的状态栏变个色!...
一、概述
近期注意到QQ新版使用了沉浸式状态栏,ok,先声明一下:本篇博客效果下图:
关于这个状态栏变色到底叫「Immersive Mode」/「Translucent Bars」有兴趣可以去了解下。
恩,接下来正题。
首先只有大于等于4.4版本支持这个半透明状态栏的效果,但是4.4和5.0的显示效果有一定的差异,所有本篇博文内容为:
如何实现半透明状态栏效果在大于4.4版本之上。
如何让4.4的效果与5.0的效果尽可能一致。
看了不少参考文章,都介绍到这个库,大家可以了解:SystemBarTint。
不过本篇博文并未基于此库,自己想了个hack,对于此库源码有空再看了。
二、效果图
先贴下效果图,以便和实现过程中做下对比
4.4 模拟器
5.x 真机
ok,有了效果图之后就开始看实现了。
三、实现半透明状态栏
因为本例使用了NavigationView,所以布局代码稍多,当然如果你不需要,可以自己进行筛减。
注意引入相关依赖:
Java代码
compile'com.android.support:appcompat-v7:22.2.1'
compile'com.android.support:support-v4:22.2.1'
compile'com.android.support:design:22.2.0'
(一)colors.xml 和 styles.xml
首先我们定义几个颜色:
res/values/color.xml
XML/HTML代码
#FF03A9F4
#FF0288D1
@color/primary_dark
下面定义几个styles.xml
注意文件夹的路径:
values/styles.xml
XML/HTML代码
@color/primary
@color/primary_dark
#FF4081
values-v19
XML/HTML代码
true
ok,这个没撒说的。注意我们的主题是基于NoActionBar的,android:windowTranslucentStatus这个属性是v19开始引入的。
(二)布局文件
activity_main.xml
XML/HTML代码
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"
>
android:id="@+id/id_main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
android:id="@+id/id_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:fitsSystemWindows="true"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
android:id="@+id/id_tv_content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
android:text="HelloWorld"
android:textSize="30sp"/>
android:id="@+id/id_nv_menu"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/header_just_username"
app:menu="@menu/menu_drawer"
/>
DrawerLayout内部一个LinearLayout作为内容区域,一个NavigationView作为菜单。
注意下Toolbar的高度设置为wrap_content。
然后我们的NavigationView中又依赖一个布局文件和一个menu的文件。
header_just_username.xml
XML/HTML代码
android:layout_width="match_parent"
android:layout_height="192dp"
android:background="?attr/colorPrimaryDark"
android:orientation="vertical"
android:padding="16dp"
android:fitsSystemWindows="true"
android:theme="@style/ThemeOverlay.AppCompat.Dark">
android:id="@+id/id_link"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="16dp"
android:text="http://blog.csdn.net/lmj623565791"/>
android:id="@+id/id_username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/id_link"
android:text="Zhang Hongyang"/>
android:layout_width="72dp"
android:layout_height="72dp"
android:layout_above="@id/id_username"
android:layout_marginBottom="16dp"
android:src="@mipmap/ic_launcher"/>
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的代码
Java代码
package com.zhy.colorfulstatusbar;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
publicclass MainActivityextends AppCompatActivity
{
@Override
protectedvoid onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.id_toolbar);
setSupportActionBar(toolbar);
//StatusBarCompat.compat(this, getResources().getColor(R.color.status_bar_color));
//StatusBarCompat.compat(this);
}
}
没撒说的,就是setSupportActionBar。
那么现在4.4的效果图是:
其实还不错,有个渐变的效果。
现在5.x的效果:
可以看到5.x默认并非是一个渐变的效果,类似是一个深一点的颜色。
再看看我们md的规范
状态栏应该是一个比Toolbar背景色,稍微深一点的颜色。
这么看来,我们还是有必要去为4.4做点适配工作,让其竟可能和5.x显示效果一致,或者说尽可能符合md的规范。
四、调整4.4的显示方案
那么问题来了?如何做呢?
咱们这么看,4.4之后加入windowTranslucentStatus的属性之后,也就是我们可以用到状态栏的区域了。
既然我们可以用到这块区域,那么我们只要在根布局去设置一个与状态栏等高的View,设置背景色为我们期望的颜色就可以了。
于是有了以下的代码:
Java代码
package com.zhy.colorfulstatusbar;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by zhy on 15/9/21.
*/
publicclass StatusBarCompat
{
privatestaticfinalint INVALID_VAL = -1;
privatestaticfinalint COLOR_DEFAULT = Color.parseColor("#20000000");
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
publicstaticvoid compat(Activity activity,int statusColor)
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
{
if (statusColor != INVALID_VAL)
{
activity.getWindow().setStatusBarColor(statusColor);
}
return;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && Build.VERSION.SDK_INT
{
int color = COLOR_DEFAULT;
ViewGroup contentView = (ViewGroup) activity.findViewById(android.R.id.content);
if (statusColor != INVALID_VAL)
{
color = statusColor;
}
View statusBarView =new View(activity);
ViewGroup.LayoutParams lp =new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
getStatusBarHeight(activity));
statusBarView.setBackgroundColor(color);
contentView.addView(statusBarView, lp);
}
}
publicstaticvoid compat(Activity activity)
{
compat(activity, INVALID_VAL);
}
publicstaticint getStatusBarHeight(Context context)
{
int result =0;
int resourceId = context.getResources().getIdentifier("status_bar_height","dimen","android");
if (resourceId >0)
{
result = context.getResources().getDimensionPixelSize(resourceId);
}
return result;
}
}
代码的思路很简单,根据Activity找到android.R.content,在其中添加一个View(高度为statusbarHeight,背景色为我们设置的颜色,默认为半透明的黑色)。
那么只需要在Activity里面去写上:
Java代码
StatusBarCompat.compat(this);
就可以了。
如果你希望自己设置状态看颜色,那么就用这个方法:
Java代码
StatusBarCompat.compat(this, getResources().getColor(R.color.status_bar_color));
这样的话我们就解决了4.4到5.x的适配问题,一行代码解决,感觉还是不错的。
最后提一下,对于5.0由于提供了setStatusBarColor去设置状态栏颜色,但是这个方法不能在主题中设置windowTranslucentStatus属性。所以,可以编写一个value-v21文件夹,里面styles.xml写入:
XML/HTML代码
其实就是不要有windowTranslucentStatus属性。
接下来,对于默认的效果就不测试了,参考上面的效果图。
我们测试个设置状态栏颜色的,我们这里设置个红色。
4.4 模拟器
5.x 真机
ok,这样就结束啦~~
友情推荐:
怎么把android的状态栏改成蓝色,教程分享:如何实现Android沉浸式状态栏——教你让你的状态栏变个色!...相关推荐
- android studio项目改成android.mk编译APP闪退java.lang.UnsatisfiedLinkError: dalvik.system.PathClassLoader
android9系统android studio项目改成用android.mk编译v7包--APP闪退java.lang.UnsatisfiedLinkError: dalvik.system.Pat ...
- photoshop2022中文版怎么改成英文版?ps中文版改成英文版教程
很多用户在本站下载了ps动作插件,但是ps动作必须在photoshop英文版中进行,如果你photoshop是中文版怎么改成英文版呢?那么Mac电脑如何将ps中文切换英文呢?今天小编啦和大分享ps中文 ...
- android原生app转成web,转战WebApp: 最适合Android开发者的WebApp框架
原文出处:林法鑫的博客. ps:原以为又是一篇前端从业者人云亦云的水文,其实是篇干货,文中提到的AndroidUI4Web也是作者开发的. 为什么需要转战WebApp开发 随着移动端设备越来越多, 微 ...
- pycharm怎样改成中文教程
相信pycharm不会改成中文是一件很头疼的事情,特别是对一些新手来说 下面放教程: 点击"File" 点击"Settings" 在settings中找到Chi ...
- android开发:一个工具类搞定所有机型实现沉浸式状态栏
1.在build.gradle引入依赖 implementation 'com.readystatesoftware.systembartint:systembartint:1.0.3' 2.导入工具 ...
- android api版本 闪退_经验分享:从事 Android 开发六年,我学到的那些事
你的代码质量应该随着经验的增加而提高,在本文作者基于 Android 开发者六年间,其都学到了什么? 以下为译文: 六年来,我为多家公司编写过各种应用程序.你编写代码或处理代码库的文化和方式会随着你的 ...
- android电视如何打开adb调试模式,分享解密某Android电视adb后门方法
[Java] 纯文本查看 复制代码private int[] mTurnONADBKeyCode = new int[]{21, 21, 19, 22}; private int[] mTurnONA ...
- Android开发类似苹果iOS 7的沉浸式状态栏
1.什么是沉浸式状态栏? 沉浸式状态栏是指Activity的ActionBar的颜色和状态栏的颜色一样,两者连接起来了,看起来就像是一个整体,跟苹果的ios7以后的样式一样.如下图所示: (沉浸式状态 ...
- Android实现沉浸式(透明)状态栏适配
第一种讲解: 在Android系统4.4以前,状态栏的背景色和字体颜色都是不能改变的.但是4.4以后Google增加了改变状态栏背景透明的方法,可以通过两种方式来设置. 直接在Activity中设置W ...
最新文章
- 浅析企业自助建站系统的特性
- CoreJava 笔记总结-第五章 继承
- VMware下安装ubuntu,桥接上网配置静态IP
- 各大 Android 手机厂商获取 OAID 开源项目!
- 安全合规/法案--35--《APP收集使用个人信息自评估指南(征求意见稿)》原文及解读
- 《机关单位办公自动化应用指南 (基于国产信息技术应用创新终端)》
- 中文文本情感分析:基于机器学习方法的思路
- 苹果开发的资源URL链接
- 30 年的 Hello world
- PDF转换器用什么好?这款一定能够帮到你
- linux修改禅道端口,CentOS7上安装了gitlab和禅道,改了禅道端口访问不了
- IDC机房动力环境设备维护
- 花生wifi后台管理系统项目日记
- 微信小程序-弹窗提示
- 电力系统 matlab,利用matlab解决电力系统规划问题
- apche的AB测试详解
- GRUB4DOS 简介
- android 日历事件添加日程
- 并发计算中的串行思考
- 广工 AnyviewC 数据结构习题 第五章