[英]I don't want to show SystemUI when I show a alert dialog on the parent fullscreen activity
I have a full screen activity, but when I show a alert dialog above it, System UI showed (System Notification Bar).我有一个全屏活动,但是当我在它上面显示一个警报对话框时,系统 UI 显示(系统通知栏)。 Can anybody help me ?
有谁能够帮助我 ? I don't want it to be visible.
我不希望它可见。 Is there is any way I can hide the system notification bar from showing when the alert dialog appears?
有什么办法可以隐藏系统通知栏,使其在出现警报对话框时不显示?
this works for me, tested on api 21 and 16 这对我有效,已在api 21和16上测试
dialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
});
Which Context-object are you using when instantiating your Dialog? 实例化对话框时,您使用哪个上下文对象?
Maybe try using your activity? 也许尝试使用您的活动? You can do this by passing "this" as context.
您可以通过传递“ this”作为上下文来实现。
I know some people are calling getApplicationContext() which is not (always) the right way. 我知道有些人正在调用getApplicationContext(),这不是(总是)正确的方法。
Build the AlertDialog using the AlertDialog.Builder and create the AlertDialog. 使用AlertDialog.Builder构建AlertDialog并创建AlertDialog。
Prior to calling show() set the Window flags for the dialog to not focusable. 在调用show()之前,请将对话框的窗口标志设置为不可聚焦。
After showing the dialog set the SystemUiVisibility flags on the decorView of the Window representing the AlertDialog, and clear the not focusable flag. 显示对话框后,在代表AlertDialog的窗口的decorView上设置SystemUiVisibility标志,并清除不可聚焦的标志。
AlertDialog.Builder adBuilder = new AlertDialog.Builder(this);
AlertDialog alertDialog = adBuilder.setCancelable(false).
setMessage("Turn ended, Click OK").
setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
}).create();
alertDialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
alertDialog.show();
alertDialog.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
alertDialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
Now when the AlertDialog is shown the SystemUI elements don't appear. 现在,当显示AlertDialog时,不会出现SystemUI元素。 Hope this helps.
希望这可以帮助。
The other answers mostly use the flags for the setSystemUiVisibility() method in View.其他答案主要使用视图中 setSystemUiVisibility() 方法的标志。 However, this API is deprecated since Android 11.
但是,此 API 自 Android 11 起已弃用。
Here are code snippets for showing / hiding system bars with the new API as well as the deprecated one for backward compatibility:以下是使用新 API 以及已弃用的 API 显示/隐藏系统栏的代码片段:
fun Window.hideSystemUI() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
insetsController?.let {
// Default behavior is that if navigation bar is hidden, the system will "steal" touches
// and show it again upon user's touch. We just want the user to be able to show the
// navigation bar by swipe, touches are handled by custom code -> change system bar behavior.
// Alternative to deprecated SYSTEM_UI_FLAG_IMMERSIVE.
it.systemBarsBehavior = WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
// make navigation bar translucent (alternative to deprecated
// WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION)
// - do this already in hideSystemUI() so that the bar
// is translucent if user swipes it up
// window.navigationBarColor = getColor(R.color.internal_black_semitransparent_light)
// Finally, hide the system bars, alternative to View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
// and SYSTEM_UI_FLAG_FULLSCREEN.
it.hide(WindowInsets.Type.systemBars())
}
} else {
// 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
@Suppress("DEPRECATION")
decorView.systemUiVisibility = (
// Do not let system steal touches for showing the navigation bar
View.SYSTEM_UI_FLAG_IMMERSIVE
// Hide the nav bar and status bar
or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
or View.SYSTEM_UI_FLAG_FULLSCREEN
// Keep the app content behind the bars even if user swipes them up
or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN)
// make navbar translucent - do this already in hideSystemUI() so that the bar
// is translucent if user swipes it up
@Suppress("DEPRECATION")
addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION)
}
}
/**
* Shows the system bars and returns back from fullscreen.
* @see hideSystemUI
* @see addSystemUIVisibilityListener
*/
fun Window.showSystemUI() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
// show app content in fullscreen, i. e. behind the bars when they are shown (alternative to
// deprecated View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION and View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN)
setDecorFitsSystemWindows(false)
// finally, show the system bars
insetsController?.show(WindowInsets.Type.systemBars())
} else {
// Shows the system bars by removing all the flags
// except for the ones that make the content appear under the system bars.
@Suppress("DEPRECATION")
decorView.systemUiVisibility = (
View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.