首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >切换全屏模式

切换全屏模式
EN

Stack Overflow用户
提问于 2011-10-08 04:57:07
回答 5查看 15.9K关注 0票数 21

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

代码语言:javascript
复制
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之前调用,对吗?

..。但是,我在setContentView之前使用了requestWindowFeature(Window.FEATURE_NO_TITLE);,它去掉了标题和状态栏……有人能帮上忙吗?

-编辑-好的,我有一个bug,导致它不能工作。所以它确实是这样的。现在,我只需要知道如何切换标题栏。

EN

回答 5

Stack Overflow用户

发布于 2013-04-26 15:01:23

代码语言:javascript
复制
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);
}
票数 29
EN

Stack Overflow用户

发布于 2015-05-14 04:02:12

代码语言:javascript
复制
/**
 * 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);
}

我的代码在Android2.3和4.4.2上运行良好

票数 9
EN

Stack Overflow用户

发布于 2014-01-22 11:21:13

自Jellybean (4.1)以来,有了一种不依赖于WindowManager的新方法。与使用WindowManager标志相比,在窗口之外使用setSystemUiVisibility可以对系统栏进行更精细的控制。下面是启用全屏的方法:

代码语言:javascript
复制
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();
}

下面是如何还原上面的代码:

代码语言:javascript
复制
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();
}
票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7692789

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档