Android 沉浸式状态栏攻略 让你的状态栏变色吧

article/2025/8/24 8:23:53

转载请标明出处:
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,所以布局代码稍多,当然如果你不需要,可以自己进行筛减。

注意引入相关依赖:

 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 version="1.0" encoding="utf-8"?>
<resources><color name="primary">#FF03A9F4</color><color name="primary_dark">#FF0288D1</color><color name="status_bar_color">@color/primary_dark</color>
</resources>

下面定义几个styles.xml

注意文件夹的路径:

values/styles.xml

<resources><style name="BaseAppTheme" parent="Theme.AppCompat.Light.NoActionBar"><!-- Customize your theme here. --><item name="colorPrimary">@color/primary</item><item name="colorPrimaryDark">@color/primary_dark</item><item name="colorAccent">#FF4081</item></style><!-- Base application theme. --><style name="AppTheme" parent="@style/BaseAppTheme"></style>
</resources>

values-v19

<resources><style name="AppTheme" parent="@style/BaseAppTheme"><item name="android:windowTranslucentStatus">true</item></style>
</resources>

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

(二)布局文件

activity_main.xml

<android.support.v4.widget.DrawerLayout
    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"><LinearLayout
        android:id="@+id/id_main_content"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><android.support.v7.widget.Toolbar
            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"/><TextView
            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"/></LinearLayout><android.support.design.widget.NavigationView
        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"/>
</android.support.v4.widget.DrawerLayout>

DrawerLayout内部一个LinearLayout作为内容区域,一个NavigationView作为菜单。
注意下Toolbar的高度设置为wrap_content。

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

header_just_username.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"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"><TextView
        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"/><TextView
        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"/><ImageView
        android:layout_width="72dp"android:layout_height="72dp"android:layout_above="@id/id_username"android:layout_marginBottom="16dp"android:src="@mipmap/ic_launcher"/></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的代码

package com.zhy.colorfulstatusbar;import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;public class MainActivity extends AppCompatActivity
{@Overrideprotected void 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,设置背景色为我们期望的颜色就可以了。

于是有了以下的代码:

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.*/
public class StatusBarCompat
{private static final int INVALID_VAL = -1;private static final int COLOR_DEFAULT = Color.parseColor("#20000000");@TargetApi(Build.VERSION_CODES.LOLLIPOP)public static void 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 < Build.VERSION_CODES.LOLLIPOP){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);}}public static void compat(Activity activity){compat(activity, INVALID_VAL);}public static int 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里面去写上:

StatusBarCompat.compat(this);

就可以了。

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

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

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

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

<resources><!-- Base application theme. --><style name="AppTheme" parent="@style/BaseAppTheme"></style>
</resources>

其实就是不要有windowTranslucentStatus属性。

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

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

  • 4.4 模拟器

  • 5.x 真机

ok,这样就结束啦~~

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


欢迎关注我的微博:
http://weibo.com/u/3165018720


群号:463081660,欢迎入群

微信公众号:hongyangAndroid
(欢迎关注,第一时间推送博文信息)

参考

  • http://blog.raffaeu.com/archive/2015/04/11/android-and-the-transparent-status-bar.aspx
  • https://mindofaandroiddev.wordpress.com/2013/12/28/making-the-status-bar-and-navigation-bar-transparent-with-a-listview-on-android-4-4-kitkat/
  • http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2014/1117/1992.html
  • http://developer.android.com/intl/zh-cn/reference/android/view/View.html#attr_android:fitsSystemWindows

http://chatgpt.dhexx.cn/article/g6ZBh1G3.shtml

相关文章

Android 最新实现沉浸式状态栏的效果

博主前些天发现了一个巨牛的人工智能学习网站&#xff0c;通俗易懂&#xff0c;风趣幽默&#xff0c;忍不住也分享一下给大家&#xff0c; &#x1f449;点击跳转到网站 什么是状态栏: 是指手机屏幕最顶上&#xff0c;显示中国移动、安全卫士、电量、网速等等&#xff0c;在手…

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

国内很多类似的文章, 我只想说一个字, 真tm乱! 我看不懂… 评论里面 全在说无效什么的 (我试了也无效, 好厉害的样子) 不废话,回到正题, 首先贴上一个众所周知的库 SystemBarTint 我只要这个类 https://github.com/jgilfelt/SystemBarTint/blob/master/library/src/com/readys…

Android---沉浸式状态栏

Android—沉浸式状态栏 我们的征程是星辰大海&#xff0c;而非人间烟尘 文章目录 Android---沉浸式状态栏去掉标题栏效果 引入依赖沉浸状态栏颜色沉浸状态栏图片 去掉标题栏 首先去掉对应主题下面的Android自带的ActionBar&#xff0c;只需要在对应主题下面加NoActionBar 效果 …

史上最完美的Android沉浸式状态导航栏攻略

前言 最近我在小破站开发一款新App&#xff0c;叫高能链。我是一个完美主义者&#xff0c;所以不管对架构还是UI&#xff0c;我都是比较抠细节的&#xff0c;在状态栏和导航栏沉浸式这一块&#xff0c;我还是踩了挺多坑&#xff0c;费了挺多精力的。这次我将我踩坑&#xff0c…

Android设置“沉浸式状态栏”的方法

Android系统界面构成 Android系统界面由状态栏、ActionBar和导航栏组成&#xff0c;如下图所示&#xff1a; “沉浸式状态栏”的界面显示效果 沉浸式状态栏&#xff0c;或叫“透明状态栏”&#xff0c;显示效果如下图所示&#xff1a; 实现沉浸式状态栏的步骤 一、添加StatusB…

Android沉浸式状态栏(透明系统状态栏)

Android沉浸式状态栏&#xff08;透明系统状态栏&#xff09;的目的&#xff1a;顶部系统状态栏和App的导航栏一体化&#xff0c;不给用户突兀的感觉&#xff0c;使用户把更多的视角留在我们的App上。 沉浸式状态栏的兼容情况 Android版本透明状态栏<4.44.4-5.0✓>5.0✓…

沉浸式状态栏

沉浸式状态栏 简单介绍 沉浸式状态栏其实是对沉浸模式(Immersive Mode)在状态栏上的体现,即让状态栏变透明,或者是在打游戏和看视频时彻底隐藏起状态栏,让用户拥有良好的体验。 比如说我们看视频,状态栏隐藏后视频的观看会更加完整,下图就是看视频时的截图,可以看到当…

Android沉浸式状态栏,看完这篇就够了!

背景 之前做过Android沉浸式状态栏的相关需求&#xff0c;但是一直忙于工作&#xff0c;没时间系统的整理下沉浸式相关的知识&#xff0c;所以今天抽出时间&#xff0c;写一篇 Android沉浸式状态栏的文章。 何为沉浸式 沉浸式就是要给用户提供完全沉浸的体验&#xff0c;使用…

图片预览与下载

后端返回base64进行图片的预览和下载下载 有一个图片预览的接口。后端返回的值长这个样子 1、预览 如果要在线预览的话&#xff0c;代码需要这样写放入img的src this.imgPreviewBaseUrl data:image/png;base64, res.data标签中就是这样的 2、下载 图片预览的这样的返…

vue 实现图片预览

vue实现图片预览 现在很多的项目里面图片展示缩略图,然后点击实现图片预览,放大的功能 最近我的项目里面就遇见了这么个场景,我选用了插件进行处理 下面说下实现步骤 1、首先安装插件 npm install vue-photo-preview --save插件地址 2、在main.js里面引入,全局使用 impor…

图片预览器

此预览器&#xff0c;一次只能翻页十张图片&#xff0c;开发人员可根据自己需要&#xff0c;修改前台代码。 页面代码 <% Page Language"C#" AutoEventWireup"true" CodeBehind"UpFilesAndPreview.aspx.cs" Inherits"WebFramework.Comm…

v-viewer图片预览

安装 npm install v-viewer引入 import Viewer from v-viewer import viewerjs/dist/viewer.css Vue.use (Viewer, {defaultOptions: {navbar: true,toolbar: false,movable: false,title: false,zoomable: true,scalable: true,transition: true} })使用 方法一&#xff1a…

vue2图片预览插件

文章目录 学习链接准备工作准备图片与基础的样式PrevImg.vueApp.vue静态效果 封装插件修改PrevImg.vue定义插件逻辑main.js中应用此插件App.vue中使用此插件 图片预览列表修改App.vue修改插件逻辑修改PrevImg.vue 学习链接 vue插件开发实例-图片预览插件 vue2-pre-img-plugin的…

vue 图片预览插件

vue 图片预览插件 步骤 1、先安装依赖 npm install v-viewer --save2、main.js内引用并注册调用 //main.js import Viewer from v-viewer import viewerjs/dist/viewer.cssVue.use(Viewer); Viewer.setDefaults({Options: { "inline": true, "button": …

Vue3图片预览(Image)

本图片预览组件主要包括以下功能&#xff1a; 展示图片时&#xff0c;可设置鼠标悬浮时的预览文本&#xff1b;图像无法加载时要显示的描述&#xff1b;自定义图像高度和宽度&#xff1b;设置图像如何适应容器高度和宽度&#xff08; fill(填充) | contain(等比缩放包含) | cov…

Vue实现图片预览和缩放功能

使用v-viewer插件即可实现图片预览和缩放功能。&#xff08;v-viewer是一款支持vue项目中的图片浏览组件&#xff0c;它支持图片旋转、缩放、翻转等操作&#xff0c;支持配置化.非常强大&#xff09; 安装依赖 执行命令&#xff1a; npm install v-viewer --save 引入并使用…

图片上传预览功能

大多时候我们上传图片&#xff0c;都是直接上传到服务器上&#xff0c;然后返回图片资源所在服务器的路径&#xff0c;然后页面根据这个路径&#xff0c;给图片一个src 属性就能看到图片了。 但是&#xff0c;这样会有一个弊端&#xff1a;如果客户对自己上传的图片不满意&am…

uniapp 图片预览实现

uniapp 图片预览实现 提示&#xff1a;下面案例可供参考 一、app端 使用 uni.previewImage 进行图片预览 可去 uniapp官网 查看具体使用:(https://uniapp.dcloud.net.cn/api/media/image.html#unipreviewimageobject) 二、具体使用如下&#xff08;示例&#xff09;&#xf…

Android中点击图片进行图片预览功能的实现(ImagePreview图片预览工具库)

因为需求&#xff0c;对于轮播图进行一个点击预览的功能&#xff0c;看了好多的文章&#xff0c;都没有弄出来&#xff0c;还是多亏了昊昊猿博主 点击查看原文 终于把它搞定啦。 这里的话是使用ImagePreview&#xff0c;一个非常好用的图片预览工具库&#xff08;还可以实现双…

Vue实现图片预览(Viewer.js)

Viewer.js viewer.js是一款开源的图片预览插件&#xff0c;功能十分强大: 支持移动设备触摸事件 支持响应式 支持放大/缩小 支持旋转&#xff08;类似微博的图片旋转&#xff09; 支持水平/垂直翻转 支持图片移动 支持键盘 支持全屏幻灯片模式&#xff08;可做屏保&#xff0…