记录踩过的坑,希望帮到你!
//composable获取
@Composable
fun StatusBarHeight(): Int {
val insets = WindowInsets.statusBars
// insets.getBottom(LocalDensity.current)获取到的数值不能作为px直接使用,必须要进行转换
val dp = with(LocalDensity.current) { insets.getTop(LocalDensity.current).toDp() }
return dp.value.roundToPx()
}
//kotlin获取
fun BottomBarHeight(): Int {
val insets = ViewCompat.getRootWindowInsets(window.decorView)
//获取到的数值不能作为px直接使用,必须要进行转换
val statusBarHeight = insets?.getInsets(WindowInsetsCompat.Type.statusBars())?.top ?: 0
return statusBarHeight.toDp().value.roundToPx()
}