7

任何人都知道这是一个错误还是应该这样做。在使用 KitKat 的沉浸式模式时单击溢出图标时,它会禁用沉浸式模式。还有其他人遇到这个吗?

谷歌的完整代码 -在这里

public void toggleHideyBar() {

    // The UI options currently enabled are represented by a bitfield.
    // getSystemUiVisibility() gives us that bitfield.
    int uiOptions = getActivity().getWindow().getDecorView().getSystemUiVisibility();
    int newUiOptions = uiOptions;
    boolean isImmersiveModeEnabled =
            ((uiOptions | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY) == uiOptions);
    if (isImmersiveModeEnabled) {
        Log.i(TAG, "Turning immersive mode mode off. ");
    } else {
        Log.i(TAG, "Turning immersive mode mode on.");
    }

    // Navigation bar hiding:  Backwards compatible to ICS.
    if (Build.VERSION.SDK_INT >= 14) {
        newUiOptions ^= View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
    }

    // Status bar hiding: Backwards compatible to Jellybean
    if (Build.VERSION.SDK_INT >= 16) {
        newUiOptions ^= View.SYSTEM_UI_FLAG_FULLSCREEN;
    }

    // Immersive mode: Backward compatible to KitKat.
    // Note that this flag doesn't do anything by itself, it only augments the behavior
    // of HIDE_NAVIGATION and FLAG_FULLSCREEN.  For the purposes of this sample
    // all three flags are being toggled together.
    // Note that there are two immersive mode UI flags, one of which is referred to as "sticky".
    // Sticky immersive mode differs in that it makes the navigation and status bars
    // semi-transparent, and the UI flag does not get cleared when the user interacts with
    // the screen.
    if (Build.VERSION.SDK_INT >= 18) {
        newUiOptions ^= View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
    }

    getActivity().getWindow().getDecorView().setSystemUiVisibility(newUiOptions);
}
4

4 回答 4

3

在 K 中,溢出菜单是一个单独的窗口,它获得焦点,因此驱动当前系统 UI 标志。

但是,仅当您在隐藏状态栏时尝试显示操作栏时才会出现这种情况,这是不鼓励的。对于操作栏之外的上下文菜单,您可以改用PopupWindowPopupWindow您可以根据需要自行设置系统 UI 标志)。

于 2013-12-17T16:44:08.050 回答
2

我不清楚如何为 PopupWindow 设置系统 UI 标志。我试过这样的事情:

PopupMenu popupMenu = new PopupMenu(mainHandle, view) {
   @Override
   public void show() {
       getWindow().getDecorView().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);
       super.show();
}
于 2015-04-24T22:16:49.437 回答
0

这是 Kotlin 中的一种解决方法,当 ActionBar 弹出菜单被关闭时,它会启用沉浸式模式:

override fun onCreate(savedInstanceState: Bundle?) {
    ...
    supportActionBar!!.addOnMenuVisibilityListener { isVisible ->
        if (!isVisible) enableImmersiveMode()
    }
    ...
}

@SuppressLint("InlinedApi")
private fun enableImmersiveMode() {
    window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
    window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY or
            View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or
            View.SYSTEM_UI_FLAG_FULLSCREEN or
            View.SYSTEM_UI_FLAG_LAYOUT_STABLE or
            View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
}
于 2017-12-01T21:36:20.040 回答
0

对我来说效果很好的东西-希望这将帮助那些将来回到这个问题的人。

在溢出菜单出现后,您可以使用处理程序更改标志。在我的设备上,后退按钮仍然出现,但黑色导航栏不可见(在出现之前消失)。

// Creates the PopupMenu.
PopupMenu popup = new PopupMenu(getContext(), view)
{
    @Override
    public void show()
    {
        // Shows the menu.
        super.show();

        // Sets the UI flags to prevent weird changing of window.
        Handler temp = new Handler();
        temp.postDelayed(new Runnable()
        {
            @Override
            public void run()
            {
                getWindow().getDecorView().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);
            }
        }, 50);
    }
}
于 2019-10-02T14:29:11.023 回答