简体   繁体   English

如何在android中隐藏系统导航栏

[英]How to hide the system navigation bar in android

I have followed the Android developers guide on: how to hide the system navigation bar.我遵循了 Android 开发人员指南:如何隐藏系统导航栏。

int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
          | View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);

It works OK, until I show an AlertDialog.它工作正常,直到我显示一个 AlertDialog。 When the Dialog is displayed, the navigation bar (the three icons: square, triangle and circle) are displayed on top of the app's controls.当对话框显示时,导航栏(三个图标:正方形、三角形和圆形)显示在应用程序控件的顶部。

NOTE : System Navigation will still appear when ever you show any AlertDialog but when you dismiss it , It will hide again.注意:当您显示任何 AlertDialog 时,系统导航仍然会出现,但是当您关闭它时,它会再次隐藏。 If you still not want this behavior then use Center View to create Alert like view.如果您仍然不想要这种行为,请使用中心视图创建类似警报的视图。

You can try following approach which i have used .您可以尝试以下我使用过的方法。

/**
     * Hide system NavigationBar and StatusBar
     */
    @TargetApi(Build.VERSION_CODES.KITKAT)
    public void hideNavigationBar()
    {
        final View decorView = getWindow().getDecorView();
        decorView.setOnSystemUiVisibilityChangeListener (new View.OnSystemUiVisibilityChangeListener() {
            @Override
            public void onSystemUiVisibilityChange(int visibility) {
                Log.i("LOG","Menu Shown is this"+ visibility);
                decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_IMMERSIVE);

            }
        });
    }

Call above method from onResume of Activity and make sure you override following method in activity.Activity onResume调用上述方法,并确保在Activity覆盖以下方法。

@Override
    @TargetApi(Build.VERSION_CODES.KITKAT)
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus)
        {
            getWindow().getDecorView().setSystemUiVisibility(
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_IMMERSIVE
                            );
        }
    }

You should use following approach to hide navigation when Alert created.创建警报时,您应该使用以下方法隐藏导航。

public static void showAlert(Context context,String title,String message) {
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
        alertDialogBuilder.setTitle(title);
        alertDialogBuilder.setMessage(message);
        alertDialogBuilder.setCancelable(false);
        alertDialogBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                dialog.cancel();
            }
        });
       final AlertDialog alertDialog = alertDialogBuilder.create();
        //Here's the magic..
//Set the dialog to not focusable (makes navigation ignore us adding the window)
        alertDialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);

        alertDialog.show();
        //Set the dialog to immersive
        alertDialog.getWindow().getDecorView().setSystemUiVisibility(
                ((Activity)context).getWindow().getDecorView().getSystemUiVisibility());

//Clear the not focusable flag from the window
       alertDialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
    }

I found above working code from this SO我从这个SO 中找到了上面的工作代码

试试看没有官方文档

use these methods to show/hide systemUI使用这些方法来显示/隐藏systemUI

  public void hideUI() {
     decorView.setSystemUiVisibility(
        View.SYSTEM_UI_FLAG_LAYOUT_STABLE
        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
        | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav bar
        | View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar
        | View.SYSTEM_UI_FLAG_IMMERSIVE);
  }


  public void showUI() {
      decorView.setSystemUiVisibility(
        View.SYSTEM_UI_FLAG_LAYOUT_STABLE
        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM