2015-12-22 126 views
13

我試圖實現全屏模式,但對於Android 4.4以上版本,它顯示有一個空格:沉浸模式顯示空白空間

身臨其境的模式(全屏)

enter image description here

and AFTER the toggleFullScreen(false);

enter image description here

,你可以看到,它不刪除。下面是我使用切換它的代碼:您需要此標記添加到您的視圖View.SYSTEM_UI_FLAG_LAYOUT_STABLE

public void toggleFullscreen(boolean fs) { 
     if (Build.VERSION.SDK_INT >= 11) { 
      // The UI options currently enabled are represented by a bitfield. 
      // getSystemUiVisibility() gives us that bitfield. 
      int uiOptions = this.getWindow().getDecorView().getSystemUiVisibility(); 
      int newUiOptions = uiOptions; 
      boolean isImmersiveModeEnabled = 
        ((uiOptions | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY) == uiOptions); 
      if (isImmersiveModeEnabled) { 
       Log.i(getPackageName(), "Turning immersive mode mode off. "); 
      } else { 
       Log.i(getPackageName(), "Turning immersive mode mode on."); 
      } 

      // Navigation bar hiding: Backwards compatible to ICS. 
      if (Build.VERSION.SDK_INT >= 14) { 
       newUiOptions ^= View.SYSTEM_UI_FLAG_HIDE_NAVIGATION; 
      } 

      // Status bar hiding: Backwards compatible to Jellybean 
      if (Build.VERSION.SDK_INT >= 16) { 
       newUiOptions ^= View.SYSTEM_UI_FLAG_FULLSCREEN; 
      } 

      // Immersive mode: Backward compatible to KitKat. 
      // Note that this flag doesn't do anything by itself, it only augments the behavior 
      // of HIDE_NAVIGATION and FLAG_FULLSCREEN. For the purposes of this sample 
      // all three flags are being toggled together. 
      // Note that there are two immersive mode UI flags, one of which is referred to as "sticky". 
      // Sticky immersive mode differs in that it makes the navigation and status bars 
      // semi-transparent, and the UI flag does not get cleared when the user interacts with 
      // the screen. 
      if (Build.VERSION.SDK_INT >= 18) { 
       newUiOptions ^= View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY; 
      } 
      getWindow().getDecorView().setSystemUiVisibility(newUiOptions); 
     } else { 
      // for android pre 11 
      WindowManager.LayoutParams attrs = getWindow().getAttributes(); 
      if (fs) { 
       attrs.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN; 
      } else { 
       attrs.flags &= ~WindowManager.LayoutParams.FLAG_FULLSCREEN; 
      } 
      this.getWindow().setAttributes(attrs); 
     } 

     try { 
      // hide actionbar 
      if 
        (this instanceof AppCompatActivity) { 
       if (fs) getSupportActionBar().hide(); 
       else getSupportActionBar().show(); 
      } else if 
        (Build.VERSION.SDK_INT >= 11) { 
       if (fs) getActionBar().hide(); 
       else getActionBar().show(); 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
+0

您是否找到了解決方案。我面臨同樣的問題 – glo

+0

@glo下面給出的解決方案正在爲我工​​作。檢查[這](https://gitlab.com/ankit_aggarwal/ToggleFullScreenDemo.git)代碼 –

+0

@glo是這種情況發生在android l及以上版本或更低版本嗎?如果是這樣,它是隻發生在狀態或底部導航欄嗎? –

回答

24

請檢查您的佈局是否沒有android:fitsSystemWindows="true"

至少它解決了我的情況 - 我在FrameLayout上安裝了SystemWindows。

0

。嘗試像這樣

// 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. 
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); 
} 

// 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() { 
mDecorView.setSystemUiVisibility(
     View.SYSTEM_UI_FLAG_LAYOUT_STABLE 
     | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION 
     | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN); 
} 
+0

hi @android_Muncher,它沒有解決,=(,即時通訊使用片段,但從活動調用togglefullscreen,仍然顯示一個灰色的空間,其中軟按鈕是和通知欄的黑色空間 – user2582318

+0

從我共享的示例mDecorView應該你的視圖需要全屏顯示,不管它是片段還是活動,你都可以通過使用這個留置代碼來獲得當前視圖。 –

3

我在這裏是新來的,所以我不能評論,但想添加一些令我如此沮喪的我對上述解決方案感到沮喪。我一直在檢查我的活動和它的片段android:fitsSystemWindows="true",它絕對不在那裏,但我一直在底部有一個空白!我瘋了!解決這個簡單的事情不是很難!

原來,它也出現在導航抽屜裏,我加了...所以一定要檢查所有的XML!