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

前端之家收集整理的这篇文章主要介绍了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相关文章