How to set the position and transparency in the android dialog box

Source: Internet
Author: User

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 you can set the alpha value of the dialog box to transparent or semi-transparent. 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:
// Display transparent dialog box
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 );

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.