当您以某个角度握住设备时,横向的 displayCutout 不正确

问题描述

所以我们可以得到例如安全插入最高值(对于有缺口/切口的设备很有用):

override fun onAttachedToWindow() {
    super.onAttachedToWindow()
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
        val safeInsetTop = window.decorView.rootWindowInsets?.displayCutout?.safeInsetTop
        if (DEBUG) Timber.d("onAttachedToWindow,safeInsetTop: $safeInsetTop")
    }
}

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    if (DEBUG) Timber.d("onCreate")

    if (prefAlwaysLandscape) {
        requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
        if (resources.configuration.orientation != Configuration.ORIENTATION_LANDSCAPE) {
            return // activity will be recreated
        }
    }
    ...
}

当它正常工作时(当你把手机伸直时 - 没有角度):

enter image description here

日志

onAttachedToWindow,safeInsetTop: 0
onCreate

当效果不佳时(当您以某个角度握住手机时):

enter image description here

日志

onAttachedToWindow,safeInsetTop: 112
onCreate

你可以看到有112个值而不是0,这是愚蠢的,横向顶级安全值不能超过0,没有缺口,只能纵向

所以当手机以一定角度和活动开始时,它认为它处于纵向,但后来配置更改(横向)但我坚持使用旧的 112 值,这对于横向是不正确的

我不知道这个问题的解决方案

window.decorView.rootWindowInsets?.displayCutout?.safeInsetTop 只能从 onAttachedToWindow 调用以返回非空值

onCreateonStartonResume 中,它将返回 null...

更新

我从 WindowInsets.getDisplayCutout is NULL everywhere except within onAttachedToWindow in fullscreen Java app

找到了这个 windowManager.defaultDisplay.cutout?.safeInsetTop

我可以在 onCreate 调用它,但当电话倾斜时仍然值为 112

所以不管你从哪里得到它...

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...