如何在Spinner中保持沉浸式模式?

时间:2018-03-28 12:04:13

标签: android android-spinner android-immersive

我使用沉浸式粘性模式来隐藏导航栏和操作栏:

@TargetApi(19)
private void setImmersiveMode() {
    if (Build.VERSION.SDK_INT >= 19) {
        View decorView = getWindow().getDecorView();
        int uiOptions = getImmersiveUiOptions(decorView);
        decorView.setSystemUiVisibility(uiOptions);
        ActionBar actionBar = getActionBar();
        if (null!=actionBar) {
            actionBar.hide();
        }
    }
}

触摸Spinner时,会显示navigationBar并禁用沉浸式模式。

This solution适用于Dialogs:

dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
dialog.show();
dialog.getWindow().getDecorView().setSystemUiVisibility(
context.getWindow().getDecorView().getSystemUiVisibility());
dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);

但是Spinner没有我可以覆盖的show()方法。

如何在触摸微调器时阻止系统UI显示?

编辑:这个问题是关于隐藏导航栏(BackButton,HomeButton和RecentTasksButton)。我已经在使用FLAG_FULLSCREEN

getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);   

2 个答案:

答案 0 :(得分:2)

我知道这太迟了,但是我终于找到了解决这个here的方法:

只需在微调器上调用它,然后再使用它:

getters

答案 1 :(得分:1)

对于像我一样,将开始将@Quinn粘贴的解决方案从Kotlin重写到Java的每个人,后来他们发现the linked git repo也是Java的解决方案,我将其粘贴在这里:

import android.widget.ListPopupWindow;
import android.widget.PopupWindow;
import android.widget.Spinner;

public static void avoidSpinnerDropdownFocus(Spinner spinner) {
    try {
        Field listPopupField = Spinner.class.getDeclaredField("mPopup");
        listPopupField.setAccessible(true);
        Object listPopup = listPopupField.get(spinner);
        if (listPopup instanceof ListPopupWindow) {
            Field popupField = ListPopupWindow.class.getDeclaredField("mPopup");
            popupField.setAccessible(true);
            Object popup = popupField.get((ListPopupWindow) listPopup);
            if (popup instanceof PopupWindow) {
                ((PopupWindow) popup).setFocusable(false);
            }
        }
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
}