在Android 10上,弹窗自动消失的解决方法是使用PopupWindow,并为其设置setOutsideTouchable和setFocusable属性。你可以按照以下步骤进行操作:
创建一个布局文件(例如popup_layout.xml),用于定义弹窗的内容。
在你的Activity或Fragment中,创建一个PopupWindow实例并初始化它:
View popupView = getLayoutInflater().inflate(R.layout.popup_layout, null);
PopupWindow popupWindow = new PopupWindow(popupView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setOutsideTouchable(true);
popupWindow.setFocusable(true);
popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);
这里的view是你想要在其上显示弹窗的视图。
完整的示例代码如下所示:
View popupView = getLayoutInflater().inflate(R.layout.popup_layout, null);
PopupWindow popupWindow = new PopupWindow(popupView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setOutsideTouchable(true);
popupWindow.setFocusable(true);
popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);
请确保将popup_layout.xml替换为你自己的布局文件,并将view参数替换为你想要在其上显示弹窗的视图。