【问题标题】:How do you keep the Navigation Bar hidden when opening dialogs?打开对话框时如何隐藏导航栏?
【发布时间】:2016-01-29 02:28:23
【问题描述】:

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

【问题讨论】:

  • 请添加任何屏幕截图,以便我们了解问题所在。
  • 正如我上面所说的,它只发生在对话框中。我所有的活动也会使用标志自动隐藏它

标签: android android-alertdialog navigationbar


【解决方案1】:

我终于通过覆盖我拥有的自定义对话框的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);
}

【讨论】:

  • 太棒了!这是我在 StackOverflow 上找到的最好方法。没有闪烁的导航栏!!谢谢!
  • 花了 3 个小时试图找到解决方案,这是我最后的选择。非常感谢!
  • 这个答案被低估了,花了几天时间检查我的清单、主题、活动(facepalm)
【解决方案2】:

我使用@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);

【讨论】:

  • 这是真正帮助我实现目标的解决方案。谢谢朋友
  • 此解决方案有效,但接受的解决方案无效!
猜你喜欢
  • 1970-01-01
  • 2022-10-16
  • 1970-01-01
  • 1970-01-01
  • 2023-04-01
  • 2016-10-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多