[工作积累] Android: Hide Navigation bar 隐藏导航条

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     }

 

转载于:https://www.cnblogs.com/crazii/p/4238820.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值