PopupWindow显示在BottomSheetFragment下层 如何调整为显示在上层
问题根因
层级异常主要由两个错误导致:
- 调用
showAtLocation时传入的锚点view是PopupWindow自身的contentView,没有绑定到BottomSheetFragment所在的Dialog窗口的view上,导致PopupWindow默认使用Activity的Window token,层级低于BottomSheet的Dialog窗口 - 未显式设置PopupWindow的Window层级优先级,默认层级低于Dialog类窗口
具体修复步骤
- 修改
showAtLocation的锚点view,改用BottomSheetFragment自身的根view,不要使用PopupWindow自己的contentView - 显式给PopupWindow设置更高的Window层级,保证覆盖在Dialog之上
- 可选设置透明背景避免显示异常,提升显示稳定性
修改后的完整代码
val window = PopupWindow(requireContext()) val view = layoutInflater.inflate(R.layout.activity_customer__feedback, null) window.contentView = view window.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED) window.setFocusable(true) window.setOutsideTouchable(false) // 新增:设置半透明背景,避免显示异常 window.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT)) // 新增:设置Window层级,优先级高于Dialog window.windowLayoutType = WindowManager.LayoutParams.TYPE_APPLICATION_SUB_PANEL val sendFeedback = view.findViewById<Button>(R.id.bt_sendFeedback) val feedbackMessage = view.findViewById<EditText>(R.id.feedback_message) sendFeedback.setOnClickListener { if(feedbackMessage.text.toString().exIsNotEmptyOrNullOrBlank()){ window.dismiss() presenter.callNetwork(ConstantsApi.CALL_FEEDBACK, dmiConnector = FeedbackApiCall()) } else { showToast(activity,"Please fill feedback") } } // 修改:锚点改用BottomSheetFragment自身的根view window.showAtLocation(requireView(), Gravity.CENTER, 0, 0)
内容的提问来源于stack exchange,提问作者Nikita Gupta
相关产品推荐
相关产品推荐

