问题描述
所以我们可以得到例如安全插入最高值(对于有缺口/切口的设备很有用):
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
}
}
...
}
当它正常工作时(当你把手机伸直时 - 没有角度):
日志
onAttachedToWindow,safeInsetTop: 0
onCreate
当效果不佳时(当您以某个角度握住手机时):
日志
onAttachedToWindow,safeInsetTop: 112
onCreate
你可以看到有112个值而不是0,这是愚蠢的,横向顶级安全值不能超过0,没有缺口,只能纵向
所以当手机以一定角度和活动开始时,它认为它处于纵向,但后来配置更改(横向)但我坚持使用旧的 112 值,这对于横向是不正确的
我不知道这个问题的解决方案
window.decorView.rootWindowInsets?.displayCutout?.safeInsetTop
只能从 onAttachedToWindow
调用以返回非空值
在 onCreate
、onStart
或 onResume
中,它将返回 null
...
更新
找到了这个windowManager.defaultDisplay.cutout?.safeInsetTop
我可以在 onCreate
调用它,但当电话倾斜时仍然值为 112
所以不管你从哪里得到它...
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)