问题
i have a custom dialog with following style:
<style name="webtogo_app_style" parent="@android:style/Theme.Dialog">
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
</style>
It shows a borderless dialog, and anything behind gets (slightly) darker. My designer wants that everything behind got ever more dark than Android's default, but not completely black.
Is there a setting for this at all?
The only workaround I can think of is to use a full-screen activity instead of a dialog and just fill up the whole screen with semitransparent color (e.g. #99000000) and then draw my dialog over it. Is there an easier way?
Thanks!
回答1:
All you need to do is play around with the dimAmount
field in the WindowManager.LayoutParams
:
WindowManager.LayoutParams lp = myDialog.getWindow().getAttributes();
lp.dimAmount = 0.7f
回答2:
If you are creating custom dialog with theme translucent, you have to add below line as well. and you can control dim amount using above answer's code.
myDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
For me it looks like below:
WindowManager.LayoutParams lp = myDialog.getWindow().getAttributes();
lp.dimAmount = 0.7f
myDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
回答3:
WindowManager.LayoutParams lp=getWindow().getAttributes();
//set transparency of background
lp.dimAmount=0.6f; // dimAmount between 0.0f and 1.0f, 1.0f is completely dark
//lp.width = 200;
//lp.height = 300;
myDialog.getWindow().setAttributes(lp);
myDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
回答4:
try doing this
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
where dialog is the name of the dialogue box created.
来源:https://stackoverflow.com/questions/6518882/android-make-everything-around-dialog-darker-than-default