Android中ShowcaseView显示在BottomSheetDialog后方的问题解决
问题解决:ShowcaseView在Dialog后方显示的处理方案
问题原因
Dialog本身是独立的Window,默认层级高于Activity的Window。你当前的ShowcaseView基于Activity的Context创建,会依附在Activity的Window上,因此会被Dialog的Window遮挡。
解决方案
让ShowcaseView依附于Dialog的Window而非Activity的Window,同时调整其Window层级确保覆盖Dialog,修改后的代码如下:
report.setOnClickListener(v -> { final Dialog dialog = new Dialog(MainActivity.this); dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); dialog.setContentView(R.layout.bottom_dialog_layout); EditText report_bill_type = dialog.findViewById(R.id.bll); dialog.show(); dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); dialog.getWindow().setGravity(Gravity.BOTTOM); // 获取Dialog窗口的Context来创建ShowcaseView Context dialogWindowContext = dialog.getWindow().getContext(); GuideView guideView = new GuideView.Builder(dialogWindowContext) .setTitle("Title here") .setContentText("Description here") .setGravity(smartdevelop.ir.eram.showcaseviewlib.config.Gravity.auto) .setTargetView(report_bill_type) .setDismissType(DismissType.anywhere) .build(); // 调整ShowcaseView的窗口层级,确保覆盖Dialog if (guideView.getWindow() != null) { WindowManager.LayoutParams params = guideView.getWindow().getAttributes(); // 根据系统版本设置对应的窗口类型 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { params.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY; } else { params.type = WindowManager.LayoutParams.TYPE_APPLICATION_SUB_PANEL; } params.flags |= WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE; guideView.getWindow().setAttributes(params); } guideView.show(); });
补充说明
- 使用Dialog的WindowContext创建ShowcaseView,可让其与Dialog处于同一层级体系
- Android 8.0及以上需用
TYPE_APPLICATION_OVERLAY类型,低版本用TYPE_APPLICATION_SUB_PANEL,前者需在Manifest中声明<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>权限(应用内弹窗场景通常无需动态申请)
内容的提问来源于stack exchange,提问作者user21248724
相关产品推荐
相关产品推荐

