全屏和非全屏视图造成之间切换偏移

问题描述 投票:2回答:1

我有一个应用程序,其中某些观点必须是全屏,有些则需要不能全屏。在某些情况下,我想,所以我用这个当视图负荷,使活动全屏要在状态栏下显示的背景:

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)

然而,当我在这些视图之间切换整个视图将由状态栏的大小,从全屏将非全屏整个视图过低并进入导航栏下方偏移,去其他方式导致整个视图是过高和离开位的导航栏和所述视图的底部之间的空白。

我已经打过电话invalidate()设置标志,以迫使它重新划定后,但它似乎什么也不做。是否有其他呼叫我可以修复引起的偏移通过改变窗口的标志吗?

编辑:

为了给一些更多的信息 - 我不是在活动之间进行切换,我只是其中的切换视图显示在活动。当我的看法是连接我打这个电话改变decorView标志

android kotlin
1个回答
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(),当我需要显示或不显示状态栏之间切换。

© www.soinside.com 2019 - 2024. All rights reserved.