如何完全退出沉浸式全屏模式?

19

我希望实现一个按钮来启用/禁用沉浸式全屏模式。我正在使用这些方法,但showSystemUI只是快速显示并再次隐藏...

如何完全退出沉浸模式?

我的方法:

// This snippet hides the system bars.
    @SuppressLint("NewApi")
    private void hideSystemUI() {
        try{
            // 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);
        }catch(Exception e){
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
        }
    }

    // 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.
    @SuppressLint("NewApi")
    private void showSystemUI() {
        try{
            mDecorView.setSystemUiVisibility(
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
        }catch(Exception e){
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);

            mDecorView.setVisibility(View.GONE);
            mDecorView.setVisibility(View.VISIBLE);
            WindowManager.LayoutParams attrs = getWindow().getAttributes();
            attrs.flags |= WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS;
            getWindow().setAttributes(attrs);
            mDecorView.setPadding(0, getStatusBarHeight(), 0, 0);
        }
    }

如何使内容重新出现在系统栏下方?

你在哪里调用那些方法? - mohammed momn
在 ToggleButton 的 onClick 方法中,如果 FullScreenButton 被选中,调用 hideSystemUi。如果 FullScreenButton 未被选中,则调用 showSystemUI。(记住,这仅在 fullscreenbutton 中单击时调用。) - Felipe Porge Xavier
请分享完整的代码,这样我就可以测试它。 - mohammed momn
代码只有这两个按钮调用这些方法。如果您点击全屏模式,将会调用hideSystemUI方法进入沉浸式全屏模式。但是,如果您点击“返回全屏”,则会调用showSystemUI方法,但应用程序不会回到非全屏模式... - Felipe Porge Xavier
2个回答

35

使用View.SYSTEM_UI_FLAG_VISIBLE调用setSystemUiVisibility()将清除所有标志:

getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);

4

以下是对我有效的方法:

View decorView = activity.getWindow().getDecorView();
decorView.setSystemUiVisibility(0);

我可能错了,但是调用 setSystemUiVisibility 并传入 0 似乎会清除之前应用的标志。

1
你是正确的!在这里的文档中可以了解更多信息:https://developer.android.com/training/system-ui/dim#reveal - Lincoln

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接