切换全屏模式

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (143)

我需要一些帮助来切换全屏模式。我在首选项屏幕中设置了全屏。在我的主要活动中,我有:

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,它带走了标题和状态栏......任何人都可以提供一些帮助吗?

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

提问于
用户回答回答于
/**
 * 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工作正常

用户回答回答于
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);
}

所属标签

可能回答问题的人

  • 应用案例分享

    1 粉丝490 提问5 回答
  • Hyman.W

    15 粉丝0 提问3 回答
  • 找虫虫

    5 粉丝0 提问3 回答
  • uncle_light

    5 粉丝518 提问3 回答

扫码关注云+社区

领取腾讯云代金券

玩转腾讯云 有奖征文活动