我正在尝试全屏
DialogFragment
,并且能够在点击屏幕时显示/隐藏状态和导航栏。这里是DialogFragment
class FullScreenFragment : DialogFragment() {
override fun onCreate(savedStateInstance: Bundle?) {
super.onCreate(savedStateInstance)
setStyle(STYLE_NORMAL, R.style.Theme_FullScreenDialog)
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
binding = FragmentTemplateBinding.inflate(inflater, container, false)
return binding.root
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
activity?.window?.decorView?.systemUiVisibility = (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
or View.SYSTEM_UI_FLAG_FULLSCREEN
or View.SYSTEM_UI_FLAG_IMMERSIVE)
}
}
相关主题是,
<style name="Theme.FullScreenDialog" parent="Theme.MaterialComponents.Dialog">
<item name="windowNoTitle">true</item>
<item name="android:windowIsFloating">false</item>
<item name="android:windowFullscreen">true</item>
<item name="android:fitsSystemWindows">false</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:textColor">@android:color/white</item>
</style>
完成这些步骤后,我仍然可以看到导航栏。状态栏似乎被隐藏了。知道可能是什么问题吗?
通过这种方式设置我的主题,我能够在(全屏)DialogFragment 中进入沉浸式模式,而无需更改
FLAG_NOT_FOCUSABLE
:
<style name="AppTheme" parent="Theme.MaterialComponents.NoActionBar">
<item name="android:dialogTheme">@style/Dialog</item>
...
</style>
<style name="Dialog" parent="ThemeOverlay.MaterialComponents.Dialog">
<!-- windowBackground removes padding in inherited background -->
<item name="android:windowBackground">?attr/colorSurface</item>
<item name="android:windowIsFloating">false</item>
<item name="android:windowNoTitle">true</item>
</style>
然后我进入沉浸式模式(注意
dialog?.window?.decorView
而不是activity?.window?.decorView
):
dialog?.window?.decorView?.let(ViewCompat::getWindowInsetsController)?.apply {
systemBarsBehavior =
WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
hide(WindowInsetsCompat.Type.systemBars())
}