文章目录
前言二、使用步骤1.定义2.使用3.效果前言
沉浸式状态栏对安卓6.0以上版本很简单,今天分享一个封装好的工具,供大家参考
二、使用步骤
1.定义
代码如下:
package com.example.mystudy_kotlin.utilsimport android.app.Activityimport android.graphics.Colorimport android.os.Buildimport android.view.Viewimport android.view.WindowManager/*** 状态栏设置*/object HiStatusBar {/*** darkContent true:意味着 白底黑字, false:黑底白字** statusBarColor 状态栏的背景色** translucent 沉浸式效果,也就是页面的布局延伸到状态栏之下*/fun setStatusBar(activity: Activity,darkContent: Boolean,statusBarColor: Int = Color.WHITE,translucent: Boolean) {val window = activity.windowval decorView = window.decorViewvar visibility = decorView.systemUiVisibility//5.0if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//请求系统 绘制状态栏的背景色window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)//这俩不能同时出现window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)window.statusBarColor = statusBarColor}//6.0if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {if (darkContent) {//白底黑字--浅色主题visibility = visibility or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR} else {//黑底白字--深色主题// java visibility &= ~ View.SYSTEM_UI_FLAG_LIGHT_STATUS_BARvisibility = visibility and View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR.inv()}}if (translucent) {//此时 能够使得页面的布局延伸到状态栏之下,但是状图兰的图标 也看不见了,使得状态栏的图标 恢复可见性visibility =visibility or View.SYSTEM_UI_FLAG_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE}decorView.systemUiVisibility = visibility}}
2.使用
代码如下:
HiStatusBar.setStatusBar(this,false,Color.RED,false)