切换全屏模式

时间:2011-10-07 20:57:07

标签: android fullscreen

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

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

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

5 个答案:

答案 0 :(得分:25)

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 :(得分:8)

/**
 * 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

一起正常工作

答案 2 :(得分:7)

自Jellybean(4.1)以来,有一种新方法不依赖于WindowManager。而是使用窗口的setSystemUiVisibility,这使您可以比使用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();
}

答案 3 :(得分:4)

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

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

它使用按位XOR逻辑来切换FLAG_FULLSCREEN

答案 4 :(得分:3)

我的解决方案结合了以下答案:

我将这些方法添加到我的Activity中。要切换全屏,请使用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();
        }
    }
}

就我而言,我想要一个菜单​​按钮进行切换。问题:在没有硬件菜单按钮的设备上,隐藏操作栏也会隐藏切换以从全屏返回。所以,我添加了一些额外的逻辑,因此如果设备有硬件菜单按钮,它只隐藏操作栏。请注意,运行SDK 11-13的设备没有。

    if (Build.VERSION.SDK_INT >= 14 
        && ViewConfiguration.get(this).hasPermanentMenuKey()))) {

        if (full) {
            getActionBar().hide();
        } else {
            getActionBar().show();
        }
    }

较旧的设备(运行Gingerbread或更早版本)具有标题栏而不是操作栏。以下代码将隐藏它,但请注意,一旦活动开始,标题栏就无法显示/隐藏。我在帮助菜单中向用户添加了一条消息,指出在重新启动应用程序/活动之前,对全屏设备的更改可能不会对旧设备产生完全影响(当然,假设您坚持选择并仅在他们需要时执行此代码全屏)。

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