简体   繁体   English

当我在父全屏活动上显示警报对话框时,我不想显示 SystemUI

[英]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.

相关问题 在警报对话框中,我要显示选定的值 - In alert dialog box, I want to show the selected value 在片段中,我想单击返回如何显示警报对话框 - In fragment I want to click back how to show alert dialog 当我从一项活动转到另一项活动时,如何显示“警报”对话框? - How to show Alert dialog when i go from one activity to other? 用户登录后,除非用户注销,否则我不想再次显示登录活动 - After user signs in I don't want sign in activity to show up again unless user logs out Android Studio不会在“设备选择器”对话框中显示我的设备。 我不想使用模拟器 - Android Studio doesn't show my device in the “Device Chooser” dialog. I don't want to use the emulator 吐司消息,我不想显示。 可能吗? - toast message I DON'T WANT TO SHOW. is it possible? 如何防止打开新活动时显示对话框的父活动? - How to prevent parent activity of a dialog to show when opening new activity? 当用户wifi关闭时,我不想在Android应用中显示我的webviews网址 - I don't want to show my webviews url in Android apps when users wifi is off 为什么我想进行第二项活动时程序显示调用目标异常? - why program show invocationtargetexception when i want to go second activity? Android类设计-每次我想向他们展示对话框类时,我都应该重新实例化它们吗? - Android class design - should I reinstantiate dialog classes in my activity each time I want to show them?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM