我在 Android 11 中遇到了全屏沉浸模式的問題。我的主要活動布局檔案看起來像這樣,
<DrawerLayout .... android:fitsSystemWindows="true">
<CoordinatorLayout>
<AppBarLayout>
<FragmentContainerView>
我正在嘗試從我的 Fragment 中顯示全屏模式。在 Android 11 之前,我曾經從 Fragment 的 onCreate 視圖中呼叫以下函式
fun hideStatusBar (activity: AppCompatActivity?) {
activity?.window?.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN) }
我用,
fun hideStatusBar(activity: AppCompatActivity?) {
@Suppress("DEPRECATION")
if (isAtLeastAndroid11()) {
val controller = activity?.window?.insetsController
controller?.hide(WindowInsets.Type.statusBars())
} else {
activity?.window?.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
}
}
這會按預期洗掉狀態欄,但會在狀態欄區域的螢屏頂部留下空白區域
帶狀態欄:

移除狀態欄:

我嘗試測量片段內的視窗插入并調整片段容器的高度
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
if(isAtLeastAndroid11()){
WindowCompat.setDecorFitsSystemWindows(requireActivity().window, false)
setUiWindowInsets()
}
}
private fun setUiWindowInsets() {
ViewCompat.setOnApplyWindowInsetsListener(rootView) { _, insets ->
posTop = insets.getInsets(WindowInsetsCompat.Type.systemBars()).top
posBottom = insets.getInsets(WindowInsetsCompat.Type.systemBars()).bottom
rootView.updateLayoutParams<ViewGroup.MarginLayoutParams> {
updateMargins(
top = posTop,
bottom = posBottom)
}
insets
}
}
But my ViewCompat.setOnApplyWindowInsetsListener is never called. As per this article, Coordinator Layout consumes onApplyWindowInsets callbacks and the child won't get any callbacks. rootView is the root view of my Fragment (a relative layout) which was placed in FragmentContainer in my layout hierarchy.
Comparison between Android 10 and 11

My Question:
- How should I get the call to my
setOnApplyWindowInsetsListenermethod in fragment? - How should I let my coordinator layout know to occupy full screen when status bar is hidden?
References: Coordinator layout consumes callbacks:
- setOnApplyWindowInsetsListener never called
- https://medium.com/androiddevelopers/why-would-i-want-to-fitssystemwindows-4e26d9ce1eec
- https://newbedev.com/fitssystemwindows-effect-gone-for-fragments-added-via-fragmenttransaction
uj5u.com熱心網友回復:
問題來自應用程式欄。即使它是全屏的,它也會嘗試防止內容與狀態欄發生沖突,即使它是隱藏的。這是一個新的實作,也是為了防止手機缺口覆寫內容。
如果您想使用整個螢屏,您應該洗掉應用欄。如果確實需要操作的工具提示,請使用底部欄。
uj5u.com熱心網友回復:
您可能必須fitsSystemWindows關閉標志(windowWindow 實體在哪里):
window.setDecorFitsSystemWindows(false);
所以我喜歡這樣:
private void hideSystemUI() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
windowInsetsController.hide(WindowInsets.Type.statusBars());
window.setDecorFitsSystemWindows(false);
}
}
當然,你還需要在顯示狀態欄時設定標志:
private void showSystemUI() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
windowInsetsController.show(WindowInsets.Type.statusBars());
window.setDecorFitsSystemWindows(true);
}
}
僅供參考,您可能需要觸發hideSystemUI或showSystemUI在Activity.onWindowFocusChanged.
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus) {
if (isImmersive) {
hideSystemUI();
} else {
showSystemUI();
}
}
}
(以上為Java語言,請翻譯成Kotlin)
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/327442.html
標籤:android android-fragments android-11 安卓全屏 窗口插入
