6

我想實現一個按鈕來啓用/禁用沉浸式全屏模式。我正在使用這些方法,但showSystemUI只顯示快速,並再次隱藏...如何完全退出Immersive全屏模式?

如何完全退出沉浸模式?

我的方法:

// This snippet hides the system bars. 
    @SuppressLint("NewApi") 
    private void hideSystemUI() { 
     try{ 
      // Set the IMMERSIVE flag. 
      // Set the content to appear under the system bars so that the content 
      // doesn't resize when the system bars hide and show. 
      mDecorView.setSystemUiVisibility(
        View.SYSTEM_UI_FLAG_LAYOUT_STABLE 
          | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION 
          | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN 
          | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav bar 
          | View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar 
          | View.SYSTEM_UI_FLAG_IMMERSIVE); 
     }catch(Exception e){ 
      getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
        WindowManager.LayoutParams.FLAG_FULLSCREEN); 
     } 
    } 

    // This snippet shows the system bars. It does this by removing all the flags 
    // except for the ones that make the content appear under the system bars. 
    @SuppressLint("NewApi") 
    private void showSystemUI() { 
     try{ 
      mDecorView.setSystemUiVisibility(
        View.SYSTEM_UI_FLAG_LAYOUT_STABLE 
          | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION 
          | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN); 
     }catch(Exception e){ 
      getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); 

      mDecorView.setVisibility(View.GONE); 
      mDecorView.setVisibility(View.VISIBLE); 
      WindowManager.LayoutParams attrs = getWindow().getAttributes(); 
      attrs.flags |= WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS; 
      getWindow().setAttributes(attrs); 
      mDecorView.setPadding(0, getStatusBarHeight(), 0, 0); 
     } 
    } 

如何使內容再次出現下系統吧?

+0

在那裏你調用這些方法? –

+0

在ToggleButton的onClick方法中。如果FullScreenButton isChecked,則調用hideSystemUi。如果未選中FullScreenButton,請調用showSystemUI。 (請記住,這只是在全屏按鈕點擊才能調用。 –

+0

任何人都可以幫助我嗎? –

回答

11

調用帶有View.SYSTEM_UI_FLAG_VISIBLE setSystemUiVisibility()清除所有標誌:

getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE); 
+4

或者在View中使用常量'setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);' – Gomino