4

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

最好我希望它们被完全删除,但从我所读到的,除非你根设备,否则这是不可能的

所以我的问题基本上是:当我显示弹出菜单时,如何隐藏导航栏图标

前

后

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

  • hideNavigation()在弹出菜单显示前后调用
  • 呼入hideNavigation()_onCreate(), onResume() & onWindowFocusChanged()
  • 请求关注另一个视图
  • 试图从下拉列表中清除焦点(尝试失败,并没有真正找到方法)
  • 更改图标颜色,“伪装”它会被隐藏(尝试失败,并没有真正找到方法)
  • hideNavigation()结合使用(Handler尝试失败,也许我没有正确执行)
  • 尝试配置一些 COSU/KIOSK 模式选项(也许有一些方法可以完全禁用整个导航栏?我还没有找到隐藏后退按钮的方法)

活动


    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)

标题栏


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

弹出菜单


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

预期结果:导航栏及其图标被隐藏,弹出菜单显示后,图标仍然隐藏

实际结果:导航栏及其图标被隐藏,弹出菜单显示后,图标显示

4

2 回答 2

3

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

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

这是代码:

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()
    }
}
于 2019-01-11T08:11:05.267 回答
0

因为PopupMenu使用新的DecorView(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);
}
于 2022-02-09T03:44:26.360 回答