You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android中Window Insets返回0的问题及Compose解决方案

解决Jetpack Compose中Window Insets返回值为0的问题

很多Android开发者在Jetpack Compose中处理Window Insets时都会遇到一个常见问题——Insets返回值为0。我结合官方文档和实践经验整理了一套可靠的解决方案:

  1. 配置Activity的系统窗口适配
    在Activity的onCreate方法中添加代码,禁用系统自动调整DecorView以适配系统窗口:

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        WindowCompat.setDecorFitsSystemWindows(window, false)
    }
    
  2. 配置Manifest的软键盘调整模式
    在对应Activity的Manifest配置条目里添加以下属性:

    android:windowSoftInputMode="adjustResize"
    
  3. 在Composable中应用Insets内边距
    若要给Composable添加状态栏等安全区域的内边距,使用windowInsetsPadding Modifier:

    modifier = modifier.windowInsetsPadding(WindowInsets.safeDrawing)
    

    如需其他类型的Insets,直接调用WindowInsets类的对应属性即可(例如WindowInsets.statusBars单独获取状态栏Insets)。

Stack Overflow上大量关于Insets返回0.0dp的问题,大多是因为遗漏了前两步的基础配置。

内容的提问来源于stack exchange,提问作者Bram Jongebloet

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 18:07:23