[工作積累] Android: Hide Navigation bar 隱藏導航條

來源:互聯網
上載者:User

標籤:

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 as4 // a general rule, you should design your app to hide the status bar whenever you5 // hide the navigation bar.6 int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION7               | 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_FULLSCREEN10             | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav bar11             | View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar12             | View.SYSTEM_UI_FLAG_IMMERSIVE);13 }14 15 // This snippet shows the system bars. It does this by removing all the flags16 // 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_STABLE20             | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION21             | 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 level11 } else {12 uiFlags |= View.SYSTEM_UI_FLAG_LOW_PROFILE;13 }14 15 getWindow().getDecorView().setSystemUiVisibility(uiFlags);16 }17 18 19 //////////////////////////////////////////////////////////////////////////20 @Override21 protected void onCreate (Bundle savedInstanceState) {22 ...23 hideNavigationBar();24 super.onCreate(savedInstanceState);25 ...26 }27 28 //////////////////////////////////////////////////////////////////////////29 @Override30 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.

[工作積累] Android: Hide Navigation bar 隱藏導航條

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.