我想将状态栏颜色从透明更改为白色。它适用于21至28的所有API,但不适用于API 23:-)
我尝试了systemUiVisibility
标志的许多不同组合,但结果是:
我无法同时获得可见的白色状态栏和可见的XML布局。
fun Activity.setStatusBarColorLight(drawer: DrawerLayout? = null) {
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1)
this.window?.apply {
decorView.systemUiVisibility = 0
clearFlags(FLAG_TRANSLUCENT_NAVIGATION)
clearFlags(FLAG_TRANSLUCENT_STATUS)
addFlags(FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS or FLAG_FORCE_NOT_FULLSCREEN)
val color = AttributeUtils.obtainStyledAttributeColor(context, R.attr.colorPrimary)
statusBarColor = color
drawer?.setStatusBarBackgroundColor(color)
if (UtilsKt.isColorLight(statusBarColor) && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
}
}
}
fun Activity.setTransparentStatusBar(colorId: Int = android.R.color.transparent, drawer: DrawerLayout? = null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
this.window?.run {
val color = context.resources.getColor(colorId, theme)
decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
addFlags(FLAG_TRANSLUCENT_STATUS)
statusBarColor = color
drawer?.setStatusBarBackgroundColor(color)
}
}
}
不幸的是,似乎没有关于如何使用这些systemUiVisibility
和窗口标志及其组合的好的教程:-(