Android Spinner:如何在全屏状态下隐藏状态栏?

发布于 2025-01-09 13:09:29 字数 972 浏览 0 评论 0原文

我使用此代码来隐藏导航栏:

View root = findViewById(android.R.id.content);
root.setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);     
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);

但是当触摸微调器时,状态栏会显示在屏幕顶部。

此解决方案似乎在 android:spinnerMode="dropdown" 时有效

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

android:spinnerMode="dialog" 有解决方案吗?

I use this code to to hide the navigation bar:

View root = findViewById(android.R.id.content);
root.setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);     
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);

But when a Spinner is touched, the status bar is shown on top of the screen.

This solution seems to work when android:spinnerMode="dropdown"

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

Is there a solution for android:spinnerMode="dialog" ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

话少情深 2025-01-16 13:09:29

当重新创建弹出窗口时,每次单击微调器时都需要清除焦点;但不幸的是没有直接的 API 来监听这个事件。

为了克服这个问题,您可以创建一个自定义 Spinner &重写其 performClick(),当单击微调器以显示菜单时会触发该函数。然后您就可以在分享时清除焦点。

public class DialogSpinner extends Spinner {
    public DialogSpinner(Context context) {
        super(context);
    }

    public DialogSpinner(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public DialogSpinner(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    @Override
    public boolean performClick() {

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

        return super.performClick();

    }


}

You need to clear the focus every time you click the spinner as the popupWindow is recreated; but unfortunately there is no direct API for listening to this event.

To overcome this you can create a custom Spinner & override its performClick() which gets triggered when the spinner is clicked to show up the menu. Then you can clear the focus as you shared.

public class DialogSpinner extends Spinner {
    public DialogSpinner(Context context) {
        super(context);
    }

    public DialogSpinner(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public DialogSpinner(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    @Override
    public boolean performClick() {

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

        return super.performClick();

    }


}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文