我的主题有Theme.AppCompat.Dialog
个父级。事情就是我的所有活动都会隐藏导航栏,但是当打开一个对话框时,它会返回一个有时是黑色的,有时是透明的背景颜色。有没有办法在打开对话框时隐藏它?
答案 0 :(得分:4)
我终于通过覆盖我自定义对话框的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);
}
答案 1 :(得分:1)
我使用@John Ernest Guadalupe的想法通过AlertDialog解决了我的同样问题,但是通过他的解决方案,导航栏弹出了四分之一秒,然后消失了(讨厌的轻弹)。我不喜欢这样,所以我使用了一些技巧来消除它:
隐藏导航栏之前,显示对话框。
// 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);