【问题标题】:Android: Spinner dropdown in full screenAndroid:全屏微调下拉菜单
【发布时间】:2017-08-22 12:03:59
【问题描述】:

我正在尝试以全屏(沉浸式模式)打开下拉微调器,但问题是当下拉菜单打开时,它会在底部显示半透明导航栏。选择选项时导航栏会隐藏,但只要下拉菜单可见,导航栏就会保持可见。 我能够在对话框片段中删除此行为,因为我有 show(FragmentManager manager, String tag) 方法来覆盖和添加它

getDialog().getWindow().getDecorView().setSystemUiVisibility(getActivity()
.getWindow().getDecorView().getSystemUiVisibility());

// Make the dialogs window focusable
 again.getDialog().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);

但在微调器中没有类似的方法可用。我尝试使用父级中的侦听器实现将这些方法放在performClick() 中,但仍然没有运气。

此问题的任何解决方案。

【问题讨论】:

  • @UmarHussain 找到解决方案了吗?
  • 不,我找不到解决方案,所以我只是接受了这种行为。但在可能的情况下,我使用对话框片段中的列表作为替代方案。

标签: android android-spinner android-immersive android-navigation-bar


【解决方案1】:

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

Kotlin 用户使用此扩展功能

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

fun Spinner.avoidDropdownFocus() {
try {
    val listPopup = Spinner::class.java
            .getDeclaredField("mPopup")
            .apply { isAccessible = true }
            .get(this)
    if (listPopup is ListPopupWindow) {
        val popup = ListPopupWindow::class.java
                .getDeclaredField("mPopup")
                .apply { isAccessible = true }
                .get(listPopup)
        if (popup is PopupWindow) {
            popup.isFocusable = false
        }
    }
} catch (e: Exception) {
    e.printStackTrace()
}
}

您需要在您的OnCreate 方法中或当您的Spinner 膨胀时或在使用它之前的任何时间从您的spinner 调用该方法。

spinner.avoidSpinnerDropdownFocus()

感谢 kakajika GitHub 用户 kakajika https://gist.github.com/kakajika/a236ba721a5c0ad3c1446e16a7423a63

【讨论】:

  • 就像一个魅力。记得根据版本使用正确的导入;导入 androidx.appcompat.widget.AppCompatSpinner 导入 androidx.appcompat.widget.ListPopupWindow
【解决方案2】:

试试这个代码:

getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-07
    • 1970-01-01
    • 2015-05-26
    • 1970-01-01
    • 1970-01-01
    • 2014-12-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多