如何修复:当 pop-up 菜单打开时,导航栏图标仍然显示(全屏 - 沉浸式粘性)

How to fix : Navigation bar icons still showing, when pop-up menu is opened (Full screen - Immersive sticky)

我正在尝试实现一个 full-screen 应用程序,其中用户无权访问 status- & navigation-bar

最好我希望它们 完全删除,但根据我的阅读,这是不可能的,除非你对设备进行 root

所以我的问题基本上是:如何在显示 pop-up 菜单时隐藏 navigation-bar 图标

之前

之后

到目前为止,我已经尝试过:

ACTIVITY


    class PinCodeActivity, HasTitleBar {
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_pin_code)
            initTitleBarWith(this)
            hideNavigation()
        }

        override fun onResume() {
            super.onResume()
            hideNavigation()
        }

        fun hideNavigation() {
            window.decorView.apply {
                systemUiVisibility = FLAGS
            }
        }

        override fun onWindowFocusChanged(hasFocus: Boolean) {
            super.onWindowFocusChanged(hasFocus)
            hideNavigation()
        }
    }

const val FLAGS = (View.SYSTEM_UI_FLAG_LOW_PROFILE or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or View.SYSTEM_UI_FLAG_FULLSCREEN or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY)

TITLE-BAR


    fun HasTitleBar.initTitleBarWith(activity: Activity, resId: Int = R.id.titleBar) {
        val langButton = activity.findViewById<View>(resId).findViewById<Button>(R.id.tbLanguageChoiceBtn)
        val wrapper = ContextThemeWrapper(activity, R.style.MyPopupMenu)
        val popupMenu = PopUpLanguageMenu(wrapper, langButton)
        langButton.setOnClickListener {
            activity.hideNavigation()
            popupMenu.showMenu()
            activity.hideNavigation()
        }
    }

POP-UP 菜单


    class PopUpLanguageMenu constructor(context: Context, view: View) : PopupMenu(context, view) {

        private var popupHelper: MenuPopupHelper

        init {
            val popUpMenu = PopupMenu(context, view).apply {
                inflate(R.menu.menu_language_dropdown)
            }

            popupHelper = MenuPopupHelper(context, popUpMenu.menu as MenuBuilder, view)
            popupHelper.run {
                gravity = Gravity.END
                setForceShowIcon(true)
            }
        }

        fun showMenu() {
            popupHelper.show()
        }
    }

预期结果:Navigation-bar & 它的图标被隐藏,显示 pop-up 菜单后,图标仍然 HIDDEN

实际结果:Navigation-bar & 它的图标被隐藏,显示 pop-up 菜单后,图标 SHOWN

导航栏重新出现是因为有一个新的DecorView(PopupDecorView)新绘制在Views堆栈的顶部,它不受您之前设置的FLAGs的影响。

这里没有灵丹妙药,我的方法是通过反射深入 WindowManagerGlobal 并捕获 peek View,再次在其上应用系统 FLAG,因此在 PopupMenu 出现后,它设法隐藏 Navigation紧随其后的栏(仍然有一次从导航栏中显示)。

代码如下:

override fun onWindowFocusChanged(hasFocus: Boolean) {
    super.onWindowFocusChanged(hasFocus)
    if (hasFocus) {
        hideNavigation()
    } else {
        // When PopupMenu appears, the current Activity looses the focus
        setFlagsOnThePeekView() // Hijack to the current peek view, apply the Flags on it
    }
}

@SuppressLint("PrivateApi")
fun setFlagsOnThePeekView() {
    try {
        val wmgClass = Class.forName("android.view.WindowManagerGlobal")
        val wmgInstance = wmgClass.getMethod("getInstance").invoke(null)
        val viewsField = wmgClass.getDeclaredField("mViews")
        viewsField.isAccessible = true

        val views = viewsField.get(wmgInstance) as ArrayList<View>
        // When the popup appears, its decorView is the peek of the stack aka last item
        views.last().apply { 
            systemUiVisibility = FLAGS
            setOnSystemUiVisibilityChangeListener {
                systemUiVisibility = FLAGS
            }
        }
    } catch (e: Exception) {
        e.printStackTrace()
    }
}

因为PopupMenu使用了新的装饰视图(PopupDecorView),尝试:

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    if(hasFocus)
    {
        fullSceen(getWindow().getDecorView());
    }
    else {
        try {
            Object wmgInstance = Class.forName("android.view.WindowManagerGlobal").getMethod("getInstance").invoke(null);
            Field viewsField = Class.forName("android.view.WindowManagerGlobal").getDeclaredField("mViews");

            viewsField.setAccessible(true);
            ArrayList<View> views = (ArrayList<View>) viewsField.get(wmgInstance);
            for (View view: views) {
                fullSceen(view);

                view.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
                    @Override
                    public void onSystemUiVisibilityChange(int visibility) {
                        fullSceen(view);
                    }
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        AppUtils.console(this,TAG, "hasFocus == false");
    }
}
private void fullSceen(View view){
    view.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
                    | View.SYSTEM_UI_FLAG_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
}