Android: Hide Navigation bar 隐藏导航条
2016-08-01 17:37
274 查看
转载地址:http://www.cnblogs.com/crazii/p/4238820.html
https://developer.android.com/training/system-ui/navigation.html
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().
https://developer.android.com/training/system-ui/immersive.html
Note: If you like the auto-hiding behavior of IMMERSIVE_STICKY but need to show your own UI controls as well, just use IMMERSIVE combined with Handler.postDelayed() or something similar to re-enter immersive mode after a few seconds.
Current Requirement:
Hide navigation bar on startup
Auto hide navigation bar (IMMERSIVE_STICKY + poseDelayed() )
Implementation:
Note: postDelayed is not yet added, because on tested devices, navigation bar will auto hide for some secs.
Add postDelayed() event to manully auto hide nav bar if needed.
on some system, after volume key's pressed, navigation bar is shown & never hidden until keys on navi bar is pressed.
hard fix:
https://developer.android.com/training/system-ui/navigation.html
1 View decorView = getWindow().getDecorView(); 2 // Hide both the navigation bar and the status bar. 3 // SYSTEM_UI_FLAG_FULLSCREEN is only available on Android 4.1 and higher, but as 4 // a general rule, you should design your app to hide the status bar whenever you 5 // hide the navigation bar. 6 int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION 7 | View.SYSTEM_UI_FLAG_FULLSCREEN; 8 decorView.setSystemUiVisibility(uiOptions);
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().
https://developer.android.com/training/system-ui/immersive.html
1 // This snippet hides the system bars. 2 private void hideSystemUI() { 3 // Set the IMMERSIVE flag. 4 // Set the content to appear under the system bars so that the content 5 // doesn't resize when the system bars hide and show. 6 mDecorView.setSystemUiVisibility( 7 View.SYSTEM_UI_FLAG_LAYOUT_STABLE 8 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION 9 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN 10 | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav bar 11 | View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar 12 | View.SYSTEM_UI_FLAG_IMMERSIVE); 13 } 14 15 // This snippet shows the system bars. It does this by removing all the flags 16 // except for the ones that make the content appear under the system bars. 17 private void showSystemUI() { 18 mDecorView.setSystemUiVisibility( 19 View.SYSTEM_UI_FLAG_LAYOUT_STABLE 20 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION 21 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN); 22 }
Note: If you like the auto-hiding behavior of IMMERSIVE_STICKY but need to show your own UI controls as well, just use IMMERSIVE combined with Handler.postDelayed() or something similar to re-enter immersive mode after a few seconds.
Current Requirement:
Hide navigation bar on startup
Auto hide navigation bar (IMMERSIVE_STICKY + poseDelayed() )
Implementation:
1 ////////////////////////////////////////////////////////////////////////// 2 public void hideNavigationBar() { 3 int uiFlags = View.SYSTEM_UI_FLAG_LAYOUT_STABLE 4 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION 5 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN 6 | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav bar 7 | View.SYSTEM_UI_FLAG_FULLSCREEN; // hide status bar 8 9 if( android.os.Build.VERSION.SDK_INT >= 19 ){ 10 uiFlags |= 0x00001000; //SYSTEM_UI_FLAG_IMMERSIVE_STICKY: hide navigation bars - compatibility: building API level is lower thatn 19, use magic number directly for higher API target level 11 } else { 12 uiFlags |= View.SYSTEM_UI_FLAG_LOW_PROFILE; 13 } 14 15 getWindow().getDecorView().setSystemUiVisibility(uiFlags); 16 } 17 18 19 ////////////////////////////////////////////////////////////////////////// 20 @Override 21 protected void onCreate (Bundle savedInstanceState) { 22 ... 23 hideNavigationBar(); 24 super.onCreate(savedInstanceState); 25 ... 26 } 27 28 ////////////////////////////////////////////////////////////////////////// 29 @Override 30 public void onWindowFocusChanged(boolean hasFocus) { 31 super.onWindowFocusChanged(hasFocus); 32 if( hasFocus ) { 33 hideNavigationBar(); 34 } 35 }
Note: postDelayed is not yet added, because on tested devices, navigation bar will auto hide for some secs.
Add postDelayed() event to manully auto hide nav bar if needed.
on some system, after volume key's pressed, navigation bar is shown & never hidden until keys on navi bar is pressed.
hard fix:
1 ////////////////////////////////////////////////////////////////////////// 2 @Override 3 public boolean onKeyUp(int keyCode, KeyEvent event) { 4 super.onKeyUp(keyCode, event); 5 if ((keyCode == KeyEvent.KEYCODE_VOLUME_UP || keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)){ 6 this.hideNavigationBar(); 7 } 8 return false; 9 }
相关文章推荐
- Android: Hide Navigation bar 隐藏导航条
- [工作积累] Android: Hide Navigation bar 隐藏导航条
- Android hide Navigation bar
- [android] how to hide system bar and navigation bar in android
- Android NavigationBar隐藏与浮层
- Android 如何关闭Navigation Bar M
- android隐藏标题栏(titlebar)以及全屏显示
- 使用内部(com.android.internal)和隐藏(@hide)API手记
- Android ActionBar 作为导航条的一个Bug
- 左侧导航条(Left Navigation Bar)_Yahoo_Pattern(翻译)
- 使用内部(com.android.internal)和隐藏(@hide)API[第4部分,定制ADT]
- android使用隐藏api的方法(使用被@hide的api)
- 隐藏android WebView的高亮选择框 How to Hide Android WebView Highlight Border (or change it’s color)【转】
- 使用内部(com.android.internal)和隐藏(@hide)API[第2部分,定制android.jar]
- 使用内部(com.android.internal)和隐藏(@hide)API[第1部分,介绍]
- 使用内部(com.android.internal)和隐藏(@hide)API[第2部分,定制android.jar]
- android 4.x 显示、隐藏导航条
- iOS: 在navigation controller里,如何hide navigation bar of first sub controller?
- Android Hide the title bar in 2 ways
- 【Silverlight】Bing Maps学习系列(九):自定义功能导航条(Custom NavigationBar)