android背景虚化

有时候需要这种效果,弹出dialog,主背景虚化,此时需要使用的属性为:WindowManager.LayoutParams.FLAG_BLUR_BEHIND。
具体代码如下:

    final Dialog exitDialog = new Dialog(this, R.style.FullScreenDialog);
    LinearLayout ll = (LinearLayout) inflater.inflate(
            R.layout.exitdialog_layout, null);
    Button submit = (Button) ll.findViewById(R.id.submit);
    Button cancel = (Button) ll.findViewById(R.id.cancel);
    Window w = exitDialog.getWindow();
    w.setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND,
            WindowManager.LayoutParams.FLAG_BLUR_BEHIND);

    submit.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            exitDialog.dismiss();
            BaseApp.actManager.closeAllActivity();
            finish();
        }
    });
    cancel.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            exitDialog.dismiss();

        }
    });
    exitDialog.setContentView(ll);
    exitDialog.show();

此时就可以实现背景虚化了.上面只可能在4.0之前上运行,4.03后已经不支持了,可是使用这种方法

    WindowManager.LayoutParams lp = dialog2.getWindow().getAttributes();
    lp.dimAmount = 0.55f;
    dialog2.getWindow().setAttributes(lp);
    dialog2.getWindow()
            .addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);

你可能感兴趣的:(android背景虚化)