安卓实现沉浸式状态栏(通过theme主题进行设置)

综述:

沉浸式状态栏在许多app中已经被广泛使用了,项目中使用了沉浸式状态栏,有代码设置和theme主题设置两种方式,我的项目是以theme主题的方式来使用的,过程当中也遇到了不少的坑,现在把相关知识点记录下来,以备后期需要的时候使用。

本文章中的沉浸式状态栏,实际上应该表述为透明式状态栏更为准确。


在布局中需要延展到状态栏的控件下添加以下内容,表示保留status栏的空间,否则,布局的内容会挤到状态栏中。

android:fitsSystemWindows="true"


一、在6.0(23)以上:

各个item的用法已经添加了备注

效果图:

安卓实现沉浸式状态栏(通过theme主题进行设置)_第1张图片


把"android:windowLightStatusBar"项改为“false”:

效果图:

安卓实现沉浸式状态栏(通过theme主题进行设置)_第2张图片


把"android:windowTranslucentStatus"项改为"true":

    
效果图:

安卓实现沉浸式状态栏(通过theme主题进行设置)_第3张图片


二、在5.0(21)以上:

在5.0版本中,并没有"<itemname="android:statusBarColor">@android:color/transparentitem>"属性。在values-v21下的style.xml中添加如下主题:

    
效果图如下:

安卓实现沉浸式状态栏(通过theme主题进行设置)_第4张图片

把"<itemname="android:windowTranslucentStatus">trueitem>"改为true:

    
效果图如下:

安卓实现沉浸式状态栏(通过theme主题进行设置)_第5张图片



三、在4.4(19)以上:

在values-v19文件夹下的styles.xml中添加如下主题

    
由于没有机器及模拟器,没有效果图。







你可能感兴趣的:(android知识点)