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

扶醉桌前 提交于 2019-12-21 17:52:40

问题


I have a theme that has a Theme.AppCompat.Dialog parent. The thing is all my activities keeps the navigation bar hidden, but when a dialog is opened, it returns with a sometimes black and sometimes transparent background color. Is there a way to keep it hidden during opening dialogs?


回答1:


I finally solved this by overriding the show() method of the custom dialog that I have.

@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);
}



回答2:


I used @John Ernest Guadalupe's idea to solve my same problem with an AlertDialog but with his solution the navigation bar popped up for a quarter second and after gone (nasty flick). I didn't like this so i used a little trick for eliminate it:

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);


来源:https://stackoverflow.com/questions/33405901/how-do-you-keep-the-navigation-bar-hidden-when-opening-dialogs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!