gpt4 book ai didi

android - 在 fragment 更改时恢复窗口插图

转载 作者:行者123 更新时间:2023-12-05 05:53:15 25 4
gpt4 key购买 nike

我有三个 fragment 。我只想在一个 fragment 上应用透明状态栏。为此,我在底部导航栏的 setOnItemSelectedListener 方法上调用以下隐藏方法。还添加了我现在得到的图像

private fun hideStatusBar() {
window.statusBarColor = ContextCompat.getColor(this@SuperActivity, R.color.transparent)
WindowCompat.setDecorFitsSystemWindows(window, false)
ViewCompat.setOnApplyWindowInsetsListener(binding.root) { view, windowInsets ->
val insets = windowInsets.getInsets(WindowInsetsCompat.Type.navigationBars())

view.updateLayoutParams<ViewGroup.MarginLayoutParams> {
leftMargin = insets.left
rightMargin = insets.right
bottomMargin = insets.bottom
}
WindowInsetsCompat.CONSUMED
}
}


private fun showStatusBar() {
window.statusBarColor = ContextCompat.getColor(this@SuperActivity, R.color.transparent)
WindowCompat.setDecorFitsSystemWindows(window, true)
}

我在调用 hide 方法的 fragment 上得到了适当的行为。 enter image description here

但是当我点击另一个 fragment (需要显示状态栏的 fragment )时,我得到以下行为:

enter image description here

最佳答案

下边距默认为0(或根布局“binding.root”中的指定值)

因此,您需要重新设置下边距;如果它已经是 0;然后你可以:

private fun showStatusBar() {
window.statusBarColor = ContextCompat.getColor(this@SuperActivity, R.color.transparent)
WindowCompat.setDecorFitsSystemWindows(window, true)

ViewCompat.setOnApplyWindowInsetsListener(binding.root) { view, windowInsets ->
val insets = windowInsets.getInsets(WindowInsetsCompat.Type.navigationBars())

view.updateLayoutParams<ViewGroup.MarginLayoutParams> {
bottomMargin = 0 // reset the margin
}
WindowInsetsCompat.CONSUMED
}
}
}

或者如果是别的东西;然后你需要将它从 dp 转换为像素并将其设置为 bottomMargin

如果您在 binding.root 中有一些指定的边距值,同样适用;但我认为你没有,因为问题只出现在底部。

更新:

The method setOnApplyWindowInsetsListener is not called inside showStatusBar method. Because in this, the Window Insets are not changed. Since, we added margin in hideStatusBar method, so this space that you see below navigation bar is from hideStatusBar method.

虽然监听器应该被触发,但是你可以直接更新根:

binding.root.updateLayoutParams<ViewGroup.MarginLayoutParams> {
bottomMargin = 0
}

但请注意 setDecorFitsSystemWindows 可能需要一些时间来更新,因此 updateLayoutParams 不会产生效果,因此,您可能需要一点延迟:

Handler(Looper.getMainLooper()).postDelayed( {
binding.root.updateLayoutParams<ViewGroup.MarginLayoutParams> {
bottomMargin = 0
}
}, 0.1.toLong())

关于android - 在 fragment 更改时恢复窗口插图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69923207/

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