Android设置页面Activity全屏(隐藏导航栏、状态栏,深入浅出Android开发

3、代码中设置:在setContentView 之前调用

requestWindowFeature(Window.FEATURE_NO_TITLE);

getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,

WindowManager.LayoutParams.FLAG_FULLSCREEN);

注意:
Android设置页面Activity全屏(隐藏导航栏、状态栏,深入浅出Android开发_第1张图片

当有全面屏手机可以显示虚拟按键时,比如小米8手机,就会出现白色的虚拟按键区域,如图所示:

在这里插入图片描述

按照google的官方办法,设置如下几个Flag就可以隐藏导航栏:

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

google对这个方案做了说明:

  • With this approach, touching anywhere on the screen causes the navigation bar (and status bar) to reappear and remain visible. The user interaction causes the flags to be be cleared.(触摸屏幕任何位置,导航栏都会重新出现并保持可见。因为用户交互导致设置的flag被清除了

  • Once the flags have been cleared, your app needs to reset them if you want to hide the bars again. See Responding to UI Visibility Changes for a discussion of how to listen for UI visibility changes so that your app can respond accordingly.(如果想要导航栏再次隐藏,就要重新设置flag

  • Where you set the UI flags makes a difference. If you hide the system bars in your activity’s onCreate() method and the user presses Home, the system bars will reappear. When the user reopens the activity, onCreate() won’t get called, so the system bars will remain visible. If you want system UI changes to persist as the user navigates in and out of your activity, set UI flags in onResume() or onWindowFocusChanged().(不同地方设置UI Flag效果会有影响。在onResume或onWindowFouncChanged()函数里设置flag永久生效

  • The method setSystemUiVisibility() only has an effect if the view you call it from is visible.(只有在View是可见状态下,调用setSystemUiVisiblity才会生效)Navigating away from the view causes flags set with setSystemUiVisibility() to be cleared.(离开当前view,会导致利用setSystemUiVisiblity()函数设置的flag被清除

官方对这几个flag的解释:

SYSTEM_UI_FLAG_HIDE_NAVIGATION

和FLAG_FULLSCREEN、FLAG_LAYOUT_IN_SCREEN一起使用会暂时隐藏导航栏。一旦用户与界面发生交互,导航栏又会出现。

SYSTEM_UI_FLAG_IMMERSIVE_STICKY

只有和SYSTEM_UI_FLAG_FULLSCREEN、SYSTEM_UI_FLAG_HIDE_NAVIGATION其中的一个或两个一起使用时才会有效果。

FLAG_IMMERSIVE_STICKY**

只有和SYSTEM_UI_FLAG_FULLSCREEN、SYSTEM_UI_FLAG_HIDE_NAVIGATION其中的一个或两个一起使用时才会有效果。

你可能感兴趣的:(程序员,架构,移动开发,android)