我有一个用于拍照的 Fragment,我想让它处于全屏模式。在 API 30 之前,我使用此处官方文档中描述的标志。但是在将我的项目版本升级到 30 后,Android Studio 告诉该标志已弃用。所以,我搜索了很多,并在这里找到了一些类似这样的问题。按照答案,我将代码更改为:
@Suppress("DEPRECATION")
@SuppressLint("ObsoleteSdkInt")
fun setFullscreen(activity: Activity?) {
activity?.let{
when {
/*I added this case below. And the view looks very weird. Why??*/
Build.VERSION.SDK_INT >= 30 -> {
it.window.setDecorFitsSystemWindows(false)
val controller: WindowInsetsController? = it.window.insetsController
controller?.let { insetsController ->
insetsController.hide(WindowInsets.Type.statusBars() or WindowInsets.Type.navigationBars())
insetsController.systemBarsBehavior = WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
}
}
/*The code below worked for years. Why they change working stuff?*/
Build.VERSION.SDK_INT > 10 -> {
// Enables regular immersive mode.
// For "lean back" mode, remove SYSTEM_UI_FLAG_IMMERSIVE.
// Or for "sticky immersive," replace it with SYSTEM_UI_FLAG_IMMERSIVE_STICKY
it.window.decorView.systemUiVisibility = (
View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
// Set the content to appear under the system bars so that the
// content doesn't resize when the system bars hide and show.
or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
// Hide the nav bar and status bar
or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
or View.SYSTEM_UI_FLAG_FULLSCREEN
or View.SYSTEM_UI_FLAG_LOW_PROFILE
)
(it as? AppCompatActivity)?.supportActionBar?.hide()
}
else -> {
it.window
.setFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN
)
}
}
}
}
在片段中,我这样称呼这个方法:
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// some code ...
// bla bla etc.
setFullscreen(activity)
// .. and so on
}
这是一个演示:
如您所见,用户无法访问顶部的按钮,视图的底部也是白色的。我希望有人能帮帮忙。