2014-04-23 37 views
0

我使用的示例代碼的Android文檔中,使我的活動全屏:沉浸模式 - 菜單欄沒有消失

// This snippet hides the system bars. 
private void hideSystemUI() { 
    // 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. 
    getWindow().getDecorView().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_STICKY); 
} 

// 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. 
private void showSystemUI() { 
    getWindow().getDecorView().setSystemUiVisibility(
      View.SYSTEM_UI_FLAG_LAYOUT_STABLE 
        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION 
        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN); 
} 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    hideSystemUI(); 
} 

這部作品推出。當我點擊屏幕時,菜單出現,帶有透明狀態欄和不透明的導航欄。問題是,它不會超時並消失,正如文檔指出的那樣。我錯過了什麼?

回答

2

沉浸模式適用於KitKat 4.4及以上版本。我不認爲有任何第三方API可以讓它在4.4以下工作。

那麼你可以使用全屏模式爲Android < 4.4 nd身臨其境的模式。

設置uiVisibilityChangeListner。

if(android.os.Build.VERSION.SDK_INT >= 19) { 
     getWindow().getDecorView().setOnSystemUiVisibilityChangeListener(new OnSystemUiVisibilityChangeListener() { 
      @Override 
      public void onSystemUiVisibilityChange(int visibility) { 
       if(visibility == 0) { 
        getWindow().getDecorView().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 
          | View.SYSTEM_UI_FLAG_FULLSCREEN 
          | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY); 
       } 
      } 
     }); 
    } 
相關問題