在全屏和非全屏之间切换会导致视图偏移

时间:2019-01-25 21:30:09

标签: android kotlin

我有一个应用,其中某些视图需要全屏显示,而其他视图则不需要全屏显示。在某些情况下,我希望背景显示在状态栏下,因此在加载视图以使活动全屏显示时可以使用它:

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

,然后在其他视图中,我可以将其切换回非全屏状态,并以纯色显示状态栏,以便在加载这些视图时使用:

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

但是,当我在这些视图之间切换时,整个视图将被statusBar的大小所抵消,从全屏变为非全屏,整个视图太低并且低于导航栏,反之则导致整个视图视图过高,在导航栏和视图底部之间留有一些空白。

我尝试在设置标志以使其重绘后调用invalidate(),但似乎无济于事。我可以再打个电话来解决因更改窗口标志而引起的偏移吗?

编辑:

要提供更多信息-我不在活动之间切换,我只是在活动中显示哪个视图。附加我的视图后,我将发出呼叫来更改decorView标志

1 个答案:

答案 0 :(得分:0)

好吧,所以我找到了一种方法来实现这一点,不确定是否理想,但它是否可以正常工作...本质上,我只是始终使该应用程序处于全屏状态,并从内容视图中手动添加/删除填充以使其在状态栏后面展开是否。

这是实现:

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

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

然后在我的父视图中,我有以下电话:

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()。

相关问题