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

前端之家收集整理的这篇文章主要介绍了android – 如何完全退出沉浸式全屏模式?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想实现一个按钮来启用/禁用沉浸式全屏模式.我正在使用这些方法,但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);
        }
    }

如何让内容再次显示在系统栏下?

解决方法

使用View.SYSTEM_UI_FLAG_VISIBLE调用setsystemUIVisibility()会清除所有标志:
getWindow().getDecorView().setsystemUIVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
原文链接:https://www.f2er.com/android/312865.html

猜你在找的Android相关文章