简体   繁体   English

隐藏导航栏-Android培训

[英]Hiding the Navigation Bar - Android Training

I'm currently going through the Android training, because I'm trying to hide the navigation bar properly. 我目前正在接受Android培训,因为我正在尝试正确隐藏导航栏。 In the training documentation it states: 培训文档中指出:

You can hide the navigation bar on Android 4.0 and higher using the SYSTEM_UI_FLAG_HIDE_NAVIGATION flag. 您可以使用SYSTEM_UI_FLAG_HIDE_NAVIGATION标志在Android 4.0及更高版本上隐藏导航栏。 This snippet hides both the navigation bar and the status bar: 此代码段同时隐藏了导航栏和状态栏:

then they go on to provide a code sample. 然后他们继续提供代码示例。

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

As you can see it says that the SYSTEM_UI_FLAG_FULLSCREEN flag is only available in 4.1 but they say that this block of code is for 4.1. 如您所见,它说SYSTEM_UI_FLAG_FULLSCREEN标志仅在4.1中可用,但他们说此代码块适用于4.1。 Wouldn't this cause the application to crash? 这不会导致应用程序崩溃吗? Should my block of code look more like: 我的代码块应该更像是:

View decorView = getWindow().getDecorView();
if (Build.VERSION.SDK_INT == 14 || Build.VERSION.SDK_INT == 15){
    int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
    decorView.setSystemUiVisibility(uiOptions);
} else if (Build.VERSION.SDK_INT >=16) {
    int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN;
    decorView.setSystemUiVisibility(uiOptions);
}

Wouldn't this cause the application to crash? 这不会导致应用程序崩溃吗?

Not necessarily. 不必要。

Values like View.SYSTEM_UI_FLAG_FULLSCREEN are static final int . 诸如View.SYSTEM_UI_FLAG_FULLSCREEN类的值是static final int Their actual numeric value is put into the APK, not a symbol that gets looked up at runtime. 它们的实际数值会放入APK中,而不是在运行时会被查找的符号。 Hence, you won't crash just for having the number there. 因此,您不会仅仅因为那里有电话而崩溃。

What setSystemUiVisibility() will do when an unknown flag is set, though, could vary. 但是,设置未知标志时, setSystemUiVisibility()将执行的操作可能有所不同。 Usually, it will be OK, as the platform usually masks the flags to only the bit range used for the platform's API level, and so what's happening in higher-order bits is irrelevant. 通常,这是可以的,因为平台通常仅将标志屏蔽到用于平台API级别的位范围,因此,高阶位中发生的事情是无关紧要的。 However, I have not specifically tried this in the case of setSystemUiVisibility() . 但是,对于setSystemUiVisibility() ,我没有专门尝试过。 But, if Google's sample shows it running on 4.0, and if you run it on 4.0 without issue, you should be fine. 但是,如果Google的示例显示它在4.0上运行,并且如果您在4.0上运行而没有问题,那应该没问题。

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

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