Custom screen dim with Dialog

青春壹個敷衍的年華 提交于 2019-12-17 22:34:04

问题


In Android when you pop up a dialog the screen behind it dims. Is there any way to control what that looks like? For example making it dim more or less or using some kind of a pattern?


回答1:


Yes, it is. You can control it.

After creating dialog:

WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();  
lp.dimAmount=0.0f; // Dim level. 0.0 - no dim, 1.0 - completely opaque
dialog.getWindow().setAttributes(lp);

Upd: you can even add blur behind the dialog:

dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);

Blurring is deprecated since API14:

This constant was deprecated in API level 14.
Blurring is no longer supported.




回答2:


Or you can do:

dialog.getWindow().setDimAmount(0.5f);



回答3:


This solution did not work for me. There is another option, you can cancel the flag that control dimming. This code worked for me:

dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);



回答4:


Answering it quite late but I am sure things get deprecated with time so my answer would definitely help someone. First of all create a dialog:

dialog = new Dialog(ActivityName.this);
dialog .setCancelable(false);
dialog .setContentView(R.layout.dialog_layout);

Then get the window of that dialog and add a flag called FLAG_DIM_BEHIND and finally set the dim amount on the screen.

Window window = dialog.getWindow();
if(window != null){
   window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND); // This flag is required to set otherwise the setDimAmount method will not show any effect
   window.setDimAmount(0.5f); //0 for no dim to 1 for full dim
}

Then show your dialog,

dialog.show();

And before you dismiss your dialog, clear the flags:

dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);

And then dismiss:

dialog.dismiss();



回答5:


The following parameters worked for me on Android 5.1

WindowManager.LayoutParams params = new WindowManager.LayoutParams( WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.TYPE_PHONE,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_DIM_BEHIND,
                PixelFormat.TRANSLUCENT);

params.dimAmount = 0.65f;

params can be assigned to the dialog.

dialog.getWindow().addContentView(view, params)


来源:https://stackoverflow.com/questions/6257332/custom-screen-dim-with-dialog

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!