安卓开发优雅地获取系统导航栏高度
不要写什么反射系统 View 什么的啦,MIUI 让你头秃。这个适配了,发现那个又没~
使用 WindowInsets 获取系统导航栏高度
在 Activity 的 onCreate 方法中对根布局使用。
binding.root.setOnApplyWindowInsetsListener { _, insets ->
val navigationBarHeight =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
insets.getInsets(WindowInsets.Type.navigationBars()).bottom
} else {
insets.systemWindowInsetBottom
}
insets
}
navigationBarHeight 获取到状态栏高度,单位为 px 。