打开Bottom Sheet时ActionBar中书签按钮无法点击问题求助
问题解决:BottomSheetDialog弹出后ActionBar按钮无法点击
问题原因
BottomSheetDialog的默认Window会覆盖整个屏幕(包括ActionBar所在区域),并且会拦截所有外部触摸事件——哪怕你设置了setDimAmount(0.01f),底层的Window拦截逻辑依然存在,导致ActionBar上的书签按钮无法响应点击。
解决方案
方案1:修改BottomSheetDialog的Window属性
在showCorrectWrongBottomSheetDialog方法中,给BottomSheetDialog的Window添加FLAG_NOT_TOUCH_MODAL标记,允许外部区域的触摸事件穿透:
public static void showCorrectWrongBottomSheetDialog(final FragmentActivity activity, String answer, String explanation, boolean correct, String subjectType, final CallBackListener callBackListener) { try { final BottomSheetDialog dialog = new BottomSheetDialog(activity); dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); dialog.getWindow().setDimAmount(0.01f); // 添加以下两行代码解决触摸拦截问题 dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL); dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); dialog.setContentView(R.layout.correct_wrong_bottom_sheet_dialog); dialog.setCancelable(false); ConstraintLayout bottom_layout = dialog.findViewById(R.id.bottom_layout); FButton continue_button = dialog.findViewById(R.id.continue_button); TextView textview = dialog.findViewById(R.id.textview); String finalExplanation; AppCompatImageView imageView = dialog.findViewById(R.id.image); // ... 原有代码逻辑 ... } catch (Exception e) { e.printStackTrace(); } }
如果需要让BottomSheet完全避开ActionBar区域,可以额外添加Window位置偏移:
// 在setContentView之后添加 WindowManager.LayoutParams params = dialog.getWindow().getAttributes(); params.gravity = Gravity.BOTTOM; params.y = getActionBarHeight(activity); // 偏移ActionBar高度的距离 dialog.getWindow().setAttributes(params);
辅助方法:获取ActionBar高度
private static int getActionBarHeight(FragmentActivity activity) { TypedValue tv = new TypedValue(); if (activity.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) { return TypedValue.complexToDimensionPixelSize(tv.data, activity.getResources().getDisplayMetrics()); } return 0; }
方案2:改用BottomSheetDialogFragment
如果直接修改Window属性效果不佳,建议改用BottomSheetDialogFragment,它的Window管理更规范,同样可以通过重写onCreateDialog设置穿透标记:
public class CorrectWrongBottomSheetFragment extends BottomSheetDialogFragment { @NonNull @Override public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) { BottomSheetDialog dialog = (BottomSheetDialog) super.onCreateDialog(savedInstanceState); // 允许外部触摸穿透 dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL); dialog.getWindow().setDimAmount(0.01f); return dialog; } @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.correct_wrong_bottom_sheet_dialog, container, false); // 初始化按钮、文本等控件逻辑 return view; } }
在Fragment中调用弹出:
CorrectWrongBottomSheetFragment bottomSheet = new CorrectWrongBottomSheetFragment(); bottomSheet.show(getActivity().getSupportFragmentManager(), "CorrectWrongBottomSheet");
内容的提问来源于stack exchange,提问作者Priyanshu
相关产品推荐
相关产品推荐

