android 状态栏高度获取

记录踩过的坑,希望帮到你!

复制代码
//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()
    }
相关推荐
AirDroid_cn2 小时前
安卓15平板分屏比例能到1:9吗?极限分屏设置教程
android·智能手机
菜鸟国国2 小时前
还在为 Compose 屏幕适配发愁?一个 Density 搞定所有机型!
android
卡尔特斯3 小时前
Android Studio 代理配置指南
android·前端·android studio
sunbofiy233 小时前
去掉安卓的“读取已安装应用列表”,隐私合规
android
cch89183 小时前
DCATAdmin后台框架极速上手
android
Ehtan_Zheng3 小时前
ActivityMetricsLogger 深度剖析:系统如何追踪启动耗时
android
用户69371750013844 小时前
Android 开发,别只钻技术一亩三分地,也该学点“广度”了
android·前端·后端
唔664 小时前
原生 Android(Kotlin)仅串口「继承架构」完整案例二
android·开发语言·kotlin
一直都在5724 小时前
MySQL索引优化
android·数据库·mysql