【问题标题】:Immersive Mode showing blank space显示空白的沉浸式模式
【发布时间】:2015-12-22 04:26:46
【问题描述】:

我正在尝试实现全屏模式,但对于 Android 4.4 及更高版本,它会在此处显示一个空白区域:

之前沉浸式模式(全屏)

之后 toggleFullScreen(false);

如您所见,它不会删除它。这是我用来切换它的代码:

public void toggleFullscreen(boolean fs) {
        if (Build.VERSION.SDK_INT >= 11) {
            // The UI options currently enabled are represented by a bitfield.
            // getSystemUiVisibility() gives us that bitfield.
            int uiOptions = this.getWindow().getDecorView().getSystemUiVisibility();
            int newUiOptions = uiOptions;
            boolean isImmersiveModeEnabled =
                    ((uiOptions | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY) == uiOptions);
            if (isImmersiveModeEnabled) {
                Log.i(getPackageName(), "Turning immersive mode mode off. ");
            } else {
                Log.i(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;
            }
            getWindow().getDecorView().setSystemUiVisibility(newUiOptions);
        } else {
            // for android pre 11
            WindowManager.LayoutParams attrs = getWindow().getAttributes();
            if (fs) {
                attrs.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
            } else {
                attrs.flags &= ~WindowManager.LayoutParams.FLAG_FULLSCREEN;
            }
            this.getWindow().setAttributes(attrs);
        }

        try {
            // hide actionbar
            if
                    (this instanceof AppCompatActivity) {
                if (fs) getSupportActionBar().hide();
                else getSupportActionBar().show();
            } else if
                    (Build.VERSION.SDK_INT >= 11) {
                if (fs) getActionBar().hide();
                else getActionBar().show();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

【问题讨论】:

  • 您找到解决方案了吗?我也面临同样的问题
  • @glo 下面给出的解决方案对我有用。检查this代码
  • @glo 这是仅在 android L 及更高版本中发生还是在较低版本中发生?如果是这样,它是否只发生在状态或底部导航栏上?

标签: android android-immersive


【解决方案1】:

请检查您的布局中没有android:fitsSystemWindows="true"

至少它解决了我的问题——我在 FrameLayout 上有了 fitSystemWindows。

【讨论】:

  • 但是我没有使用它,但我仍然遇到这个错误,我真的不明白为什么我会遇到这个问题。
  • 天哪!我认为这条线是为了解决这个问题。它似乎也被默认添加到抽屉布局中。删除它解决了这个问题。谢谢
【解决方案2】:

我是新来的,所以无法发表评论,但想补充一些让我对上述解决方案感到非常沮丧的东西。我一直在检查我的活动及其片段是否有android:fitsSystemWindows="true",它肯定不存在,但我一直在底部有一个缺口!我快疯了!解决这个简单的事情不会这么难!

原来它也出现在我添加的导航抽屉中......所以一定要检查你所有的 XML!

【讨论】:

    【解决方案3】:

    试试这个:

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        var viewParent = view
        while (viewParent is View) {
            viewParent.fitsSystemWindows = false
            viewParent.setOnApplyWindowInsetsListener { _, insets -> insets }
            viewParent = viewParent.parent as View?
        }
    }
    

    这是做什么的? DialogFragment#onActivityCreated() 调用 Dialog#setContentView(),它将 Dialog 的视图包装在私有的“wrapInBottomSheet”中。为了设置这些包装视图的正确标志,我们希望在它们被包装后设置这些标志,例如在 super.onActivityCreated() 之后

    还可以观看此演讲,了解有关 fitSystemWindows 和窗口插图的信息。

    【讨论】:

      【解决方案4】:

      只需将布局文件中的 android:fitsSystemWindows="true" 更改为 android:fitsSystemWindows="false"。

      【讨论】:

        【解决方案5】:

        您需要将此标志添加到您的视图 View.SYSTEM_UI_FLAG_LAYOUT_STABLE。像这样试试吧

        // This snippet hides the system bars.
        private void hideSystemUI() {
        // 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);
        }
        
        // 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.
        private void showSystemUI() {
        mDecorView.setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
        }
        

        【讨论】:

        • 嗨@android_Muncher,它没有解决,=(,我使用Fragment但从Activity调用togglefullscreen,仍然显示软按钮所在的灰色空间和通知栏所在的黑色空间
        • 我分享的示例中的 mDecorView 应该是您需要全屏的视图。它是片段还是活动都没有关系。您可以使用此代码留置权获取当前视图。最终视图 decorView = getWindow().getDecorView();
        • 同样的问题你能帮忙吗?
        猜你喜欢
        • 2019-10-22
        • 2021-05-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-02-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多