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

如何判断Activity是否通过WindowCompat开启Edge-to-Edge模式?

在Fragment中判断Activity是否开启Edge-to-Edge模式的方法

当然可以获取这个状态值,核心思路是:WindowCompat.setDecorFitsSystemWindows(window, false)本质是修改Activity的Window根视图(DecorView)的fitsSystemWindows属性,因此我们可以通过读取该属性的状态来判断是否开启了Edge-to-Edge模式。

具体实现代码

Kotlin版本

// 在Fragment的生命周期方法(如onViewCreated、onActivityCreated等)中调用
val decorView = requireActivity().window.decorView
val isEdgeToEdgeEnabled = !ViewCompat.getFitsSystemWindows(decorView)

Java版本

// 在Fragment的生命周期方法中调用
View decorView = requireActivity().getWindow().getDecorView();
boolean isEdgeToEdgeEnabled = !ViewCompat.getFitsSystemWindows(decorView);

原理说明

WindowCompat.setDecorFitsSystemWindows(window, false)的内部逻辑,就是对Window的DecorView执行ViewCompat.setFitsSystemWindows(decorView, false)。因此通过ViewCompat.getFitsSystemWindows(decorView)可以直接拿到该属性的当前值:

  • 返回false表示已开启Edge-to-Edge模式
  • 返回true表示未开启(或恢复了默认状态)

注意事项

  • 确保Fragment已关联到Activity,建议在onAttach()及之后的生命周期方法中执行上述代码,避免requireActivity()抛出空指针异常。

内容的提问来源于stack exchange,提问作者Alex F.

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.07 00:52:04