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()
    }
相关推荐
杉氧2 小时前
状态管理变迁史:为什么我们放弃了 Redux 选择 Zustand?
android·前端·react native
三少爷的鞋5 小时前
别再靠 Code Review 守底线:我做了一个静态分析项目 RedLine
android
y = xⁿ14 小时前
DeepSeek Harness 学习日记:关于Agent接口,工具调用的底层实现
android·java·学习
2601_9620664916 小时前
【Sql Server】Update中的From语句,以及常见更新操作方式
android·java·数据库
用户693717500138421 小时前
曾经安卓开发人手一个的 EventBus,为什么现在没人爱用了?
android·android studio
九皇叔叔1 天前
MySQL ORDER BY 性能优化详解:Using filesort、联合索引、ASC/DESC 与覆盖索引
android·adb
OriginCoding1 天前
用 AI Agent 协作完成一个 Android TOTP 应用:从需求边界到 v1.0.0
android·ai编程
杉氧1 天前
React 灵魂:深入剖析 Hooks 与副作用(Side Effects)陷阱
android·前端·react native
又见情义1 天前
RK3568 + RTL8211F 网络唤醒(WOL)功能适配全记录
android·网络·驱动开发
用户0934077735141 天前
HarmonyOS WPS Open SDK 实践:registerApp 鉴权与就绪门禁
android·typescript·harmonyos