打开对话框时如何隐藏导航栏?

How do you keep the Navigation Bar hidden when opening dialogs?

我有一个主题有一个 Theme.AppCompat.Dialog 父主题。问题是我的所有活动都隐藏了导航栏,但是当打开对话框时,它 returns 具有时而黑色时而透明的背景色。有没有办法在打开对话框时隐藏它?

我终于通过覆盖自定义对话框的 show() 方法解决了这个问题。

@Override
public void show() {
    // Set the dialog to not focusable.
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);

    // Show the dialog with NavBar hidden.
    super.show();

    // Set the dialog to focusable again.
    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
}

我使用@John Ernest Guadalupe 的想法通过 AlertDialog 解决了我的相同问题,但在他的解决方案中,导航栏弹出了四分之一秒然后消失了(讨厌的轻弹)。我不喜欢这个所以我用了一个小技巧来消除它:

Hide the navigation bar before showing the dialog.

// Flags for full-screen mode:
static int ui_flags =
        View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
                View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
                View.SYSTEM_UI_FLAG_FULLSCREEN |
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
                View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY |
                View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;

// Set up the alertDialogBuilder:
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this)
    .setCancelable(false)
    .setIcon(R.drawable.outline_info_black_48)
    .setTitle("Bla")
    .setMessage("Blaa blabla.")
    .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    });

// Create the alertDialog:
AlertDialog alertDialog = alertDialogBuilder.create();

// Set alertDialog "not focusable" so nav bar still hiding:
alertDialog.getWindow().
    setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
             WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);

// Set full-sreen mode (immersive sticky):
alertDialog.getWindow().getDecorView().setSystemUiVisibility(ui_flags);

// Show the alertDialog:
alertDialog.show();

// Set dialog focusable so we can avoid touching outside:
alertDialog.getWindow().
    clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);