Google的官方文檔是:
https://developer.android.com/training/system-ui/navigation.html#behind
示例代碼
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);
原文:
但是,有個問題。
這樣的確能隱藏底部虛擬導航欄,但是一旦你點擊屏幕,導航欄會出現(持續1秒左右),並且消費掉你的點擊事件。如果你要點擊一個按鈕(導航欄隱藏狀態下),你需要連續點兩次。因為1秒鍾之后,導航欄又消失了,點擊屏幕事件會被再次攔截消費。
最終的解決方案
/** * 隱藏虛擬按鍵,並且全屏 */ protected void hideBottomUIMenu() { //隱藏虛擬按鍵,並且全屏 if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) { // lower api View v = this.getWindow().getDecorView(); v.setSystemUiVisibility(View.GONE); } else if (Build.VERSION.SDK_INT >= 19) { //for new api versions. View decorView = getWindow().getDecorView(); int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_FULLSCREEN; decorView.setSystemUiVisibility(uiOptions); } }
感謝原博主