微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

android – 当使用Immersive Mode和对话框时,导航.栏重新出现并调整我的布局大小

当它在 Android 4.4上运行时,我在我的应用程序中使用沉浸式模式. ( http://developer.android.com/training/system-ui/immersive.html)

我的活动确实以全屏显示,我通过使用setonsystemUIVisibilitychangelistener解决音量键问题.我也有类似的代码将对话框放入沉浸式模式.

但是,当显示对话框时,导航.酒吧跳到屏幕上,然后立即撤退.当对话框被解除时,情况更糟 – 导航.酒吧跳跃并调整背后的活动.

以下是我的支持沉浸式模式的课程.它只是在每个Activity的onResume上调用,并且在构建每个对话框时也会调用一个单独的函数.

我错过了任何旗帜或回调,还是已知的Android问题?

public class ImmersiveModeHelper {

    public ImmersiveModeHelper(Activity activity)
    {
        mActivity = activity;
    }

    @SuppressLint("NewApi")
    public void supportFullScreenImmersiveMode()
    {
        MyLog.d("ImmersiveModeHelper: supportFullScreenImmersiveMode: ");

        // Support full-screen immersive mode on Android 4.4 and up
        if (Build.VERSION.SDK_INT >= 19)
        {
            // Get the needed flags by reflection and use them
            try
            {
                final int immersiveFlag = View.class.getField("SYstem_UI_FLAG_IMMERSIVE_STICKY")
                        .getInt(null);
                final int hideNavigationFlag = View.class
                        .getField("SYstem_UI_FLAG_HIDE_NAVIGATION").getInt(null);
                final int fullScreenFlag = View.class.getField("SYstem_UI_FLAG_FULLSCREEN").getInt(
                        null);


                // Set the flags to the window decor view
                mActivity.getwindow().getDecorView()
                        .setsystemUIVisibility(immersiveFlag | hideNavigationFlag | fullScreenFlag);

                // Set a callback to be called when visibility changes
                // (workaround
                // for volume keys)
                mActivity
                        .getwindow()
                        .getDecorView()
                        .setonsystemUIVisibilitychangelistener(
                                new View.OnsystemUIVisibilitychangelistener()
                                {
                                    @Override
                                    public void onsystemUIVisibilityChange(int visibility)
                                    {
                                        MyLog.d("ImmersiveModeHelper.supportFullScreenImmersiveMode().new OnsystemUIVisibilitychangelistener() {...}: onsystemUIVisibilityChange: " +
                                                "");

                                        if ((visibility & (immersiveFlag | hideNavigationFlag)) == 0)
                                        {
                                            Handler uiHandler = UiThreadUtils.getUiHandler();
                                            uiHandler.removeCallbacks(mHidesystemUICallback);
                                            uiHandler.postDelayed(mHidesystemUICallback,HIDE_SYstem_UI_DELAY_MILLI);
                                        }
                                    }
                                });

            } catch (Exception e)
            {
                e.printstacktrace();
                MyLog.e("ImmersiveModeHelper: supportFullScreenImmersiveMode: Couldn't support immersive mode by reflection");
            }
        } else
        {
            MyLog.i("ImmersiveModeHelper: supportFullScreenImmersiveMode: not supported on this platform version");
        }
    }

    public static void supportFullScreenImmersiveModeForDialog(final Dialog dlg)
    {
        MyLog.d("ImmersiveModeHelper: supportFullScreenImmersiveModeForDialog: ");

        // Support full-screen immersive mode on Android 4.4 and up
        if (Build.VERSION.SDK_INT >= 19)
        {
            final Window dlgWindow = dlg.getwindow();

            // Get the needed flags by reflection and use them
            try
            {
                final int immersiveFlag = View.class.getField("SYstem_UI_FLAG_IMMERSIVE_STICKY")
                        .getInt(null);
                final int hideNavigationFlag = View.class
                        .getField("SYstem_UI_FLAG_HIDE_NAVIGATION").getInt(null);
                final int fullScreenFlag = View.class.getField("SYstem_UI_FLAG_FULLSCREEN").getInt(
                        null);


                // Set the flags to the window decor view
                int flags = dlgWindow.getDecorView().getsystemUIVisibility();
                flags |= (immersiveFlag | hideNavigationFlag | fullScreenFlag);
                dlgWindow.getDecorView().setsystemUIVisibility(flags);

                // Set a callback to be called when visibility changes
                // (workaround for volume keys)
                dlgWindow.getDecorView().setonsystemUIVisibilitychangelistener(
                        new View.OnsystemUIVisibilitychangelistener()
                        {
                            @Override
                            public void onsystemUIVisibilityChange(int visibility)
                            {
                                MyLog.d("ImmersiveModeHelper.supportFullScreenImmersiveModeForDialog(...).new OnsystemUIVisibilitychangelistener() {...}: onsystemUIVisibilityChange: ");
                                if ((visibility & (immersiveFlag | hideNavigationFlag)) == 0)
                                {
                                    Runnable hidesystemUICallback = new Runnable()
                                    {
                                        @Override
                                        public void run()
                                        {
                                            supportFullScreenImmersiveModeForDialog(dlg);
                                        }
                                    };

                                    Handler uiHandler = UiThreadUtils.getUiHandler();
                                    uiHandler.removeCallbacks(hidesystemUICallback);
                                    uiHandler.postDelayed(hidesystemUICallback,HIDE_SYstem_UI_DELAY_MILLI);
                                }
                            }
                        });

            } catch (Exception e)
            {
                e.printstacktrace();
                MyLog.e("ImmersiveModeHelper: supportFullScreenImmersiveMode: Couldn't support immersive mode by reflection");
            }
        } else
        {
            MyLog.i("ImmersiveModeHelper: supportFullScreenImmersiveMode: not supported on this platform version");
        }
    }

    private Activity mActivity;

    private Runnable mHidesystemUICallback = new Runnable()
    {
        @Override
        public void run()
        {
            supportFullScreenImmersiveMode();
        }
    };

    private static final int HIDE_SYstem_UI_DELAY_MILLI = 0;

}

解决方法

来自Google Api
最好包括其他系统UI标志(例如SYstem_UI_FLAG_LAYOUT_HIDE_NAVIGATION和SYstem_UI_FLAG_LAYOUT_STABLE),以防止系统栏隐藏和显示时调整内容大小.

您还应确保同时隐藏操作栏和其他UI控件.此代码段演示了如何隐藏和显示状态和导航栏,而无需调整内容大小:

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

希望这可以帮助.

原文地址:https://www.jb51.cc/android/318389.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐