如下:
ViewCompat.setOnApplyWindowInsetsListener(window.decorView, object :androidx.core.view.OnApplyWindowInsetsListener {
override fun onApplyWindowInsets(
v: View,
insets: WindowInsetsCompat
): WindowInsetsCompat {
// 获取导航栏高度
val navigationHeight = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
val navigationBars: androidx.core.graphics.Insets =
insets.getInsetsIgnoringVisibility(WindowInsetsCompat.Type.navigationBars())
navigationBars.bottom
} else {
insets.stableInsetBottom
}
// 注意这个返回值,返回对象写的不对可能造成诸如导航栏颜色设置不生效等问题
return ViewCompat.onApplyWindowInsets(v, insets)
}
})
解注册:
ViewCompat.setOnApplyWindowInsetsListener(window.decorView, null)
注意:根据源码内容,每个View只能设置一个OnApplyWindowInsetsListener