gpt4 book ai didi

android - 在全屏和非全屏之间切换导致 View 偏移

转载 作者:行者123 更新时间:2023-11-29 02:22:34 30 4
gpt4 key购买 nike

我有一个应用程序,其中某些 View 需要全屏,而其他 View 不需要全屏。在某些情况下,我希望背景显示在状态栏下方,所以我在 View 加载时使用它来使 Activity 全屏显示:

window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
window.statusBarColor = Color.TRANSPARENT

然后在其他 View 中我有这个切换回非全屏并显示一个纯色的状态栏所以我在这些 View 加载时使用它:

window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_VISIBLE or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
window.statusBarColor = ContextCompat.getColor(context, R.color.colorPrimaryDark)

然而,当我在这些 View 之间切换时,整个 View 将被状态栏的大小偏移,从全屏到非全屏整个 View 太低并且低于导航栏,反之则导致整个 View 太高,在导航栏和 View 底部之间留下一点空白。

我试过在设置标志后调用 invalidate() 以强制它重绘,但它似乎什么也没做。是否可以进行另一个调用来修复因更改窗口标志而导致的偏移量?

编辑:

为了提供更多信息 - 我没有在 Activity 之间切换,我只是切换 Activity 中显示的 View 。附加 View 后,我会调用更改 decorView 标志

最佳答案

好吧,所以我找到了一种方法来实现这一点,不确定是否理想但它有效......基本上我只是让应用程序始终处于全屏状态并从我的内容 View 中手动添加/删除填充以使其在状态栏后面扩展还是不是。

实现如下:

首先,我将窗口标志设置为以下内容:

window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_IMMERSIVE
or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN)

然后在我的父 View 中我有这些调用:

val statusBarHeight: Int
get() {
var result = 0
val resourceId = resources.getIdentifier("status_bar_height", "dimen", "android")
if (resourceId > 0) {
result = resources.getDimensionPixelSize(resourceId)
}
return result
}

private var shouldShowBlueBar: Boolean = false

private fun hideSystemUI() {
// registered main view is the content view and I'm, setting it it to have
// no padding so it goes right to the top of the screen and shows under the
// status bar
registered_main_view.setPadding(0, 0, 0 ,0)
window.statusBarColor = Color.TRANSPARENT
}

private fun showSystemUI() {
// here I put padding that is the height of the status bar to keep the
// main content view below the status bar
registered_main_view.setPadding(0, statusBarHeight, 0 ,0)
window.statusBarColor = ContextCompat.getColor(context, R.color.colorPrimaryDark)
}

override fun toggleHeaderBlue(showBar: Boolean) {
shouldShowBlueBar = showBar
if (shouldShowBlueBar) {
showSystemUI()
} else {
hideSystemUI()
}
}

override fun onWindowFocusChanged(hasFocus: Boolean) {
super.onWindowFocusChanged(hasFocus)
if (shouldShowBlueBar)
showSystemUI()
else
hideSystemUI()
}

当我需要在显示或不显示状态栏之间切换时,我只调用 toggleHeaderBlue()。

关于android - 在全屏和非全屏之间切换导致 View 偏移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54373040/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com