溢出菜单单击禁用沉浸模式 - Android 4.4 Kitkat

时间:2013-12-04 01:59:19

标签: android android-4.4-kitkat android-fullscreen

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

Google完整代码 - Here

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 个答案:

答案 0 :(得分:3)

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

但是,只有在状态栏隐藏时才显示操作栏,即discouraged},才会出现此问题。对于操作栏之外的上下文菜单,您可以改为使用PopupWindow(使用PopupWindow,您可以根据需要自行设置系统UI标记。)

答案 1 :(得分:1)

我不清楚如何为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();
}

答案 2 :(得分: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
}

答案 3 :(得分: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);
    }
}