For example, the upper or lower part of the screen. To achieve this effect. You need to obtain the Window object of the dialog box. There are multiple methods to obtain the Window object. The easiest way is to directly obtain the Window object through the getWindow method of the AlertDialog class.
Copy codeThe Code is as follows: AlertDialog dialog = new AlertDialog. Builder (this). setTitle ("title ")
. SetMessage ("message"). create ();
Window window = alertDialog. getWindow ();
Window. setGravity (Gravity. TOP); // window. setGravity (Gravity. BOTTOM );
AlertDialog. show ();
Transparent dialog box
The default dialog box is not transparent, but we can change it to transparent or translucent by setting the alpha value of the dialog box. We all know. The color is composed of R (red), G (green), and B (blue. In addition, there will be A (transparency, Alpha) to describe the color. In the color description, if this value is 0, it indicates completely transparent, and if this value is 255, it indicates opacity.
You can set the transparency of the dialog box by setting the alpha attribute of Windows. The alpha value ranges from 0 to 1.0. If the property value is 0, it indicates completely transparent. If the value is 1.0, it indicates not transparent (that is, the normal displayed dialog box ). The following code sets the alpha value to 0.3 to display transparent and non-transparent dialogs more clearly. In this example, a background image is added, and two dialogs are displayed simultaneously (one is translucent and the other is opaque ).
Copy codeThe Code is as follows: // a transparent dialog box is displayed.
AlertDialog alertDialog = new AlertDialog. Builder (this). setMessage (
"Transparent dialog box"). setPositiveButton ("OK", null). create ();
Window window = alertDialog. getWindow ();
WindowManager. LayoutParams lp = window. getAttributes ();
// Set the transparency to 0.3
Lp. alpha = 0.6f;
Window. setAttributes (lp );
AlertDialog. show ();
When using some applications, we will find that when a dialog box or some mode windows are displayed, the subsequent content will become blurred or unclear. In fact, these effects can be easily implemented in OPhone. To implement this function, we only need to set two marks of the Wndow object. The Code is as follows:
Copy codeThe Code is as follows: window. setFlags (WindowManager. LayoutParams. FLAG_BLUR_BEHIND,
WindowManager. LayoutParams. FLAG_BLUR_BEHIND );