沉浸式模式显示空格

时间:2015-12-22 04:26:47

标签: android android-immersive

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

之前沉浸式模式(全屏)

enter image description here

AFTER toggleFullScreen(false);

enter image description here

正如您所看到的,它不会 将其删除 。这是我用来切换它的代码:

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();
        }
    }

5 个答案:

答案 0 :(得分:32)

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

至少它解决了我的情况 - 我在FrameLayout上有了适合的系统窗口。

答案 1 :(得分:5)

我是新来的,所以我无法发表评论,但想添加一些让我对上述解决方案感到沮丧的事情。我一直在检查android:fitsSystemWindows="true"的活动及其片段,但它绝对不存在,但我仍然在底部留有空隙!我疯了!解决这个简单的问题并不困难!

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

答案 2 :(得分:1)

只需在布局文件中将android:fitsSystemWindows =“ true”更改为android:fitsSystemWindows =“ false”。

答案 3 :(得分:0)

您需要将此标志添加到视图View.SYSTEM_UI_FLAG_LAYOUT_STABLE。试试这个

{
  "200": {...company object...}
}

答案 4 :(得分:0)

尝试一下:

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()

之后

也请观看此演讲以获取关于fitsSystemWindows和窗口插图的信息。