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

希望这可以帮助.

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...