You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android开发:显示Dialog时如何正确实现全页面背景模糊效果

解决方案

FLAG_BLUR_BEHIND的原生机制仅会对Dialog窗口覆盖区域做模糊,不会覆盖全屏幕,你可以选择以下两种方案修改:


方案1:修改Dialog窗口参数实现全屏模糊(适配安卓12+)

直接调整Dialog的窗口尺寸为全屏,搭配安卓12新增的模糊半径配置即可实现全屏幕背景模糊,修改sort_image_dialoge_fragment代码如下:

public class sort_image_dialoge_fragment extends DialogFragment {
    @Nullable
    @org.jetbrains.annotations.Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View view = inflater.inflate(R.layout.sort_image, container, false);

        if (getDialog() != null && getDialog().getWindow() != null) {
            getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
            
            WindowManager.LayoutParams params = getDialog().getWindow().getAttributes();
            // 设置窗口为全屏尺寸,让模糊范围覆盖整个屏幕
            params.width = WindowManager.LayoutParams.MATCH_PARENT;
            params.height = WindowManager.LayoutParams.MATCH_PARENT;
            // 安卓12及以上配置模糊半径,数值越大模糊效果越强
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
                params.setBlurBehindRadius(80);
            }
            params.dimAmount = 0.2f; // 搭配半透明蒙层作为低版本兜底效果
            getDialog().getWindow().setAttributes(params);
            getDialog().getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
            getDialog().getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        }
        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
        // 避免系统默认边距限制模糊范围,强制Dialog占满全屏
        if (getDialog() != null && getDialog().getWindow() != null) {
            getDialog().getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        }
    }
}

如果需要兼容安卓12以下版本,可以在sort_image布局的根节点加入第三方模糊控件(比如BlurView)覆盖全屏即可。


方案2:模糊宿主页面布局(全版本兼容)

直接在Dialog弹出时对HomeFragment的根布局做模糊处理,Dialog关闭时取消模糊,不受系统Dialog窗口机制限制:

  1. 先修改Home_Fragment.java代码:
private View homeRootView;
TextView sortDialoge_textView;

public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    homeRootView = inflater.inflate(R.layout.fragment_home, container, false);
    sortDialoge_textView = homeRootView.findViewById(R.id.sort_text);
    
    sortDialoge_textView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // 弹出Dialog前给根布局加模糊效果
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
                homeRootView.setRenderEffect(RenderEffect.createBlurEffect(20f,20f, Shader.TileMode.CLAMP));
            }
            sort_image_dialoge_fragment sortDialoge = new sort_image_dialoge_fragment();
            // 监听Dialog关闭,移除模糊效果
            sortDialoge.setOnDismissListener(dialog -> {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
                    homeRootView.setRenderEffect(null);
                }
            });
            sortDialoge.show(requireActivity().getSupportFragmentManager(), sortDialoge.getTag());
        }
    });
    return homeRootView;
}
  1. 这种方案可以直接去掉Dialog中FLAG_BLUR_BEHIND的相关配置。

内容的提问来源于stack exchange,提问作者Vasant Raval

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.28 10:27:05