切换全屏模式

我需要一些帮助切换全屏模式。 我有一个喜好屏幕设置去全屏。 在我的主要活动中,我有:

if(mFullscreen == true) { getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN); } else { getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN); getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); } 

但是这似乎不工作,因为它需要之前调用setContentView权利?

…另外,我有requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView之前,它带走了标题和状态栏…任何人都可以提供一些帮助吗?

—编辑—好的,我有一个错误,导致这不起作用。 所以它确实如此。 现在,我只需要知道如何切换标题栏。

Solutions Collecting From Web of "切换全屏模式"

 private void setFullscreen(boolean fullscreen) { WindowManager.LayoutParams attrs = getWindow().getAttributes(); if (fullscreen) { attrs.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN; } else { attrs.flags &= ~WindowManager.LayoutParams.FLAG_FULLSCREEN; } getWindow().setAttributes(attrs); } 
 /** * toggles fullscreen mode * <br/> * REQUIRE: android:configChanges="orientation|screenSize" * <pre> * sample: * private boolean fullscreen; * ................ * Activity activity = (Activity)context; * toggleFullscreen(activity, !fullscreen); * fullscreen = !fullscreen; * </pre> */ private void toggleFullscreen(Activity activity, boolean fullscreen) { if (Build.VERSION.SDK_INT >= 11) { // The UI options currently enabled are represented by a bitfield. // getSystemUiVisibility() gives us that bitfield. int uiOptions = activity.getWindow().getDecorView().getSystemUiVisibility(); int newUiOptions = uiOptions; boolean isImmersiveModeEnabled = ((uiOptions | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY) == uiOptions); if (isImmersiveModeEnabled) { Log.i(context.getPackageName(), "Turning immersive mode mode off. "); } else { Log.i(context.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; } activity.getWindow().getDecorView().setSystemUiVisibility(newUiOptions); } else { // for android pre 11 WindowManager.LayoutParams attrs = activity.getWindow().getAttributes(); if (fullscreen) { attrs.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN; } else { attrs.flags &= ~WindowManager.LayoutParams.FLAG_FULLSCREEN; } activity.getWindow().setAttributes(attrs); } try { // hide actionbar if (activity instanceof ActionBarActivity) { if (fullscreen) ((ActionBarActivity) activity).getSupportActionBar().hide(); else ((ActionBarActivity) activity).getSupportActionBar().show(); } else if (Build.VERSION.SDK_INT >= 11) { if (fullscreen) activity.getActionBar().hide(); else activity.getActionBar().show(); } } catch (Exception e) { e.printStackTrace(); } // set landscape // if(fullscreen) activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE); // else activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT); } 

我的代码与Android 2.3和4.4.2工作正常

由于Jellybean(4.1)有一个不依赖于WindowManager的新方法。 相反,使用setSystemUiVisibilityclosures窗口,这将使您比使用WindowManager标志更精确地控制系统栏。 这是你如何启用全屏:

 if (Build.VERSION.SDK_INT < 16) { //ye olde method getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); } else { // Jellybean and up, new hotness View decorView = getWindow().getDecorView(); // Hide the status bar. int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN; decorView.setSystemUiVisibility(uiOptions); // Remember that you should never show the action bar if the // status bar is hidden, so hide that too if necessary. ActionBar actionBar = getActionBar(); actionBar.hide(); } 

这就是你如何恢复上面的代码:

 if (Build.VERSION.SDK_INT < 16) { //ye olde method getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); } else { // Jellybean and up, new hotness View decorView = getWindow().getDecorView(); // Hide the status bar. int uiOptions = View.SYSTEM_UI_FLAG_VISIBLE; decorView.setSystemUiVisibility(uiOptions); // Remember that you should never show the action bar if the // status bar is hidden, so hide that too if necessary. ActionBar actionBar = getActionBar(); actionBar.show(); } 

我的解决scheme结合了来

  • cprcrack对这个问题的回答
  • Holoeverywhere:如何以编程方式在运行时从活动中删除操作栏
  • Android:以编程方式检测设备是否具有硬件菜单button

我将这些方法添加到我的活动。 要切换全屏,请使用setFullScreen(!isFullScreen())

 public boolean isFullScreen() { return (getWindow().getAttributes().flags & WindowManager.LayoutParams.FLAG_FULLSCREEN) != 0; } @SuppressLint("NewApi") public void setFullScreen(boolean full) { if (full == isFullScreen()) { return; } Window window = getWindow(); if (full) { window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); } else { window.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); } if (Build.VERSION.SDK_INT >= 11) { if (full) { getActionBar().hide(); } else { getActionBar().show(); } } } 

在我的情况下,我想要一个菜单​​button来切换。 问题是:在没有硬件菜单button的设备上,隐藏操作栏也隐藏了从全屏返回的切换。 所以,我添加了一些额外的逻辑,所以它只隐藏操作栏,如果设备有一个硬件菜单button。 请注意,运行SDK 11-13的设备没有一个。

  if (Build.VERSION.SDK_INT >= 14 && ViewConfiguration.get(this).hasPermanentMenuKey()))) { if (full) { getActionBar().hide(); } else { getActionBar().show(); } } 

老的设备(运行姜饼或更早)有一个标题栏,而不是一个行动栏。 下面的代码将隐藏它,但请注意,活动开始后,标题栏不能显示/隐藏。 我在帮助菜单中向用户提供了一条消息,指出全屏更改可能不会在旧设备上完全生效,直到他们重新启动应用程序/活动(当然这假设您只有在需要时才继续select并执行此代码全屏)。

  // call before setContentView() if (Build.VERSION.SDK_INT < 11) { requestWindowFeature(Window.FEATURE_NO_TITLE); } 

有一个更短的切换全屏方法实现:

 private void toggleFullscreen() { WindowManager.LayoutParams attrs = getWindow().getAttributes(); attrs.flags ^= WindowManager.LayoutParams.FLAG_FULLSCREEN; getWindow().setAttributes(attrs); } 

它使用按位异或逻辑来切换FLAG_FULLSCREEN