在使用 Navigation Drawer Activity 模版的时候,遇到了通知栏无法完全沉浸的问题,尝试搜索一些现有的解决方法,但是或多或少都会存在一些问题,通过反复尝试找到找到了一种比较靠谱的思路

环境

测试模拟器:Pixel 3A

compileSdk:32

minSdk:28

targetSdk:32

创建工程



默认效果展示:



修改步骤

  1. 设置状态栏变为透明:修改主题配置
 <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>

修改为:

<item name="android:statusBarColor">@android:color/transparent</item>

修改后完整文件:

<resources>
<!-- Base application theme. -->
<style name="Theme.MyApplication" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/purple_500</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
<item name="colorOnPrimary">@color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/teal_700</item>
<item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. -->
+ <item name="android:statusBarColor">@android:color/transparent</item>
<!-- Customize your theme here. -->
</style> <style name="Theme.MyApplication.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style> <style name="Theme.MyApplication.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" /> <style name="Theme.MyApplication.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>

修改后的效果:



打开抽屉发现已经透明,但是会覆盖一层浅色阴影

  1. 在布局中增加 fitsSystemWindows 属性

app_bar_main.xmlandroidx.coordinatorlayout.widget.CoordinatorLayoutcom.google.android.material.appbar.AppBarLayout 中增加:android:fitsSystemWindows="true",这样它会自动的给View增加一个值等于状态栏高度的 PaddingTop,让它的背景颜色占据状态栏

完整代码:

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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:fitsSystemWindows="true"
tools:context=".MainActivity"> <com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
+ android:fitsSystemWindows="true"
android:theme="@style/Theme.MyApplication.AppBarOverlay"> <androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/Theme.MyApplication.PopupOverlay" /> </com.google.android.material.appbar.AppBarLayout> <include layout="@layout/content_main" /> <com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_marginEnd="@dimen/fab_margin"
android:layout_marginBottom="16dp"
app:srcCompat="@android:drawable/ic_dialog_email" /> </androidx.coordinatorlayout.widget.CoordinatorLayout>

效果:



  1. 修改 activity_main.xml,给 com.google.android.material.navigation.NavigationView 增加上 app:insetForeground="@android:color/transparent",去除抽屉状态栏浅色阴影,

效果:

  1. 兼容深色模式

切换到深色模式的效果:



修改:night/themes.xml

android:statusBarColor 设置为:@android:color/transparent

<item name="android:statusBarColor">@android:color/transparent</item>

打开抽屉发现,这个时候状态栏已经透明了,但是状态栏背景还是会有一个黑色的背景色



修改 app_bar_main.xml,在 <com.google.android.material.appbar.AppBarLayout 中增加背景色,就可以了

android:background="?attr/colorPrimary"

最终效果:

fitsSystemWindows 属性

android:fitsSystemWindows="true"官方文档描述

Boolean internal attribute to adjust view layout based on system windows such as the status bar.

If true, adjusts the padding of this view to leave space for the system windows.

Will only take effect if this view is in a non-embedded activity.

对某个 View 设置 fitsSystemWindows 为 true,本质就是给这个 View 设置了 padding,所以在 app_bar_main.xml 的 AppBarLayout 设置 fitsSystemWindows,这样可以使得 AppBarLayout 的背景可以通过 padding 延展到状态栏,通过对 AppBarLayout 设置背景,就可以到达沉浸状态栏的效果

其他常用配置

设置状态栏为浅色模式(文字为黑字)

代码:

val controller = ViewCompat.getWindowInsetsController(binding.root)
controller?.isAppearanceLightStatusBars = true

或者使用主题 xml 来定义:

<item name="android:windowLightStatusBar">true</item>

参考资料

深色主题背景

What exactly does fitsSystemWindows do?

No Action Bar & Transparent Status Bar

Android沉浸式状态栏(透明状态栏)最佳实现

透明状态栏、全屏应用、沉浸模式

NavigationView阴影

定制你的Toolbar

Android 的style和theme

安卓导航抽屉 Navigation Drawer 实现沉浸通知栏的更多相关文章

  1. Android导航抽屉-Navigation Drawer

    Google今年七月份的时候更新了他们的Google+应用,采用了新的导航方式并抛弃了navigationdrawer.一时之间,又引发了一系列关于NavigationDrawer利弊的讨论,不过对于 ...

  2. 【原创+译文】官方文档中声明的如何创建抽屉导航栏(Navigation Drawer)

    如需转载请注明出处:http://www.cnblogs.com/ghylzwsb/p/5831759.html 创建一个抽屉导航栏 抽屉式导航栏是显示在屏幕的左边缘,它是应用程序的主导航选项面板.它 ...

  3. Navigation Drawer(导航抽屉)

    目录(?)[-] 创建一个导航抽屉 创建抽屉布局 初始化抽屉列表 处理导航项选点击事件 监听导航抽屉打开和关闭事件 点击应用图标来打开和关闭导航抽屉 创建一个导航抽屉 导航抽屉是一个位于屏幕左侧边缘用 ...

  4. 【转】Navigation Drawer(导航抽屉)

    创建一个导航抽屉 创建抽屉布局 初始化抽屉列表 处理导航项选点击事件 监听导航抽屉打开和关闭事件 点击应用图标来打开和关闭导航抽屉   创建一个导航抽屉 导航抽屉是一个位于屏幕左侧边缘用来显示应用程序 ...

  5. Android官方终于支持 Navigation Drawer(导航抽屉)模式

    在2013 google IO当天,Android团的更新了Support库,新版本(V13)的Support库中新加入了几个比较重要的功能. 添加 DrawerLayout 控件,支持创建  Nav ...

  6. Android Navigation Drawer(导航抽屉)

    Google I/O 2013 Android 更新了Support库,新版本的Support库中新加入了几个比较重要的功能. 添加 DrawerLayout 控件,支持创建  Navigation ...

  7. Creating a Navigation Drawer 创建一个导航侧边栏

    The navigation drawer is a panel that displays the app’s main navigation options on the left edge of ...

  8. Android UI开发第三十二篇——Creating a Navigation Drawer

    Navigation Drawer是从屏幕的左侧滑出,显示应用导航的视图.官方是这样定义的: The navigation drawer is a panel that displays the ap ...

  9. Android开发之Navigationdrawer导航抽屉功能的实现(源码分享)

    导航抽屉(navigationdrawer)是一个从屏幕左边滑入的面板,用于显示应用的主要导航项目.用户能够通过在屏幕左边缘滑入或者触摸操作栏的应用图标打开导航抽屉. 导航抽屉覆盖在内容之上,但不覆盖 ...

随机推荐

  1. Javascript Range对象的学习

    Range对象有几个特别难理解的属性,这里学习总结下 Range.startOffset:返回一个表示 Range 起点在 startContainer 中的位置的数字.此属性的值与Range.sta ...

  2. Spring Mvc 源代码之我见 二

    上一篇简单介绍了spring mvc 的一些基本内容 和DispatcherServlet 的doc.这一篇将会继续写我对Spring Mvc 源代码的理解.直接上代码: /** * This imp ...

  3. CSS 文本控制

    one more time one more chance. 一歩重头学前端, css入门. 学习一些 CSS 文本控制的属性,防止做傻事.请大家对照下面列表检验下: 会的.不会的.似懂非懂的.笔者是 ...

  4. PAT B1042 字符统计

    请编写程序,找出一段给定文字中出现最频繁的那个英文字母. 输入格式: 输入在一行中给出一个长度不超过 1000 的字符串.字符串由 ASCII 码表中任意可见字符及空格组成,至少包含 1 个英文字母, ...

  5. android.content.res.Resources$NotFoundException: String resource ID #0x0报错

    报错:android.content.res.Resources$NotFoundException: String resource ID #0x0 原因:在setText()中使用了int型的参数 ...

  6. sql语句中 left join,right join,inner join 的区别

    看到了sql,发现好久没写sql甚是想念哈哈哈哈,好多当时学的东西都忘了,当时总结的好多的文档也怎么都找不到了..... 言归正传,找到了一张图感觉描述的还挺清晰,先贴图,再说说自己的理解. 1.LE ...

  7. Android 接入腾讯IM即时通信(详细图文)

    原文地址:Android 接入腾讯IM即时通信(详细图文) | Stars-One的杂货小窝 腾讯云IM官网文档上提供了带UI模块和不带UI模块的,本文是基于带UI模块进行了Module封装,可以方便 ...

  8. QT 如何让release生成的EXE可在他人电脑上运行(尝试了全网的方法后,这应该能帮你解决)

    这两天,迅速上手了QT并用其基于C++编写了一个含UI的小程序 跳过中间的开发阶段 当我兴致满满的要将程序打包时,却是费尽周折,搜尽全网的关于QT的打包教程,最后都不顶用. 后面自己和队友的共同发现, ...

  9. ubuntu创建pycharm快捷方式或不显示图标

    ubuntu创建pycharm快捷方式或不显示图标 删除之前残留的pycharm快捷方式文件. sudo rm /usr/share/applications/jetbrains-pycahrm.de ...

  10. Windows和ubuntu下更改pip国内镜像

    windows下更改pip国内镜像 # 在C:\Users\admin路径下创建pip文件夹,然后创建pip.ini文件, 并在文件下写入 [global] index-url = http://py ...