2015-12-22 67 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!