简体   繁体   English

启用/禁用Android导航栏

[英]Enable/Disable Android Navbar

I want to make an app, with which you can simply switch between Hardware-Buttons and the Onscreen-navbar. 我想制作一个应用程序,您可以用它简单地在“硬件按钮”和“屏幕导航栏”之间切换。 It can have root access or be installed as system app. 它可以具有root用户访问权限,也可以作为系统应用程序安装。 How can I enable the Navbar in Java? 如何在Java中启用导航栏? How can I disable hardware-Buttons? 如何禁用硬件按钮?

(See CyanogenMod11S) (请参阅CyanogenMod11S)

Thanks! 谢谢!

You can hide the navigation bar on Android 4.0 and higher using the 您可以使用以下命令在Android 4.0及更高版本上隐藏导航栏

 SYSTEM_UI_FLAG_HIDE_NAVIGATION

flag. 旗。

This snippet hides both the navigation bar and the status bar: 此代码段同时隐藏了导航栏和状态栏:

View decorView = getWindow().getDecorView();
// Hide both the navigation bar and the status bar.
// SYSTEM_UI_FLAG_FULLSCREEN is only available on Android 4.1 and higher, but as
// a general rule, you should design your app to hide the status bar whenever you
// hide the navigation bar.
int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
          | View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);

For official documentation refer Hiding the Navigation Bar 有关官方文档,请参见隐藏导航栏

If you permanently want to hide navigation bar for Android 4.4+, which is actually called Immersive Full-Screen Mode, then you can try this snippet of code 如果您永久要隐藏Android 4.4+的导航栏(实际上称为沉浸式全屏模式),则可以尝试以下代码段

private int currentApiVersion;

@Override
@SuppressLint("NewApi")
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    currentApiVersion = android.os.Build.VERSION.SDK_INT;

    final int flags = 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
    | View.SYSTEM_UI_FLAG_FULLSCREEN
    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;

// This work only for android 4.4+
if(currentApiVersion >= Build.VERSION_CODES.KITKAT)
{

    getWindow().getDecorView().setSystemUiVisibility(flags);

    // Code below is to handle presses of Volume up or Volume down.
    // Without this, after pressing volume buttons, the navigation bar will
    // show up and won't hide
    final View decorView = getWindow().getDecorView();
    decorView
        .setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener()
        {

            @Override
            public void onSystemUiVisibilityChange(int visibility)
            {
                if((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0)
                {
                    decorView.setSystemUiVisibility(flags);
                }
            }
        });
   }

 }


 @SuppressLint("NewApi")
 @Override
 public void onWindowFocusChanged(boolean hasFocus)
 {
super.onWindowFocusChanged(hasFocus);
if(currentApiVersion >= Build.VERSION_CODES.KITKAT && hasFocus)
{
    getWindow().getDecorView().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
            | View.SYSTEM_UI_FLAG_FULLSCREEN
            | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
    }
}

For official documentation refer Using Immersive Full-Screen Mode 有关官方文档,请参阅使用沉浸式全屏模式

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

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