Android Development dialog box Advanced app

Source: Internet
Author: User

Android Development dialog box Advanced app

Creating and displaying a dialog box is simple, but if you want to do something more advanced, you need some tricks. Below you will share some of the advanced techniques used in the dialog box.


1. Change the display location of the dialog box:

In most cases, the dialog box is displayed in the middle of the screen (depending on the version of the system, it may vary). In fact, you can also display the dialog box on the left, middle, right, top, bottom, or even anywhere in the screen as needed.

To get the display of the dialog box, you need to get the window object for the dialog box and control the display of the dialog box through some methods of the window object.

For example:

Alertdialog alertdialog=new Alertdialog.builder (this). Setmessage ("Show dialog at top of screen"). Setpositivebutton ("OK", null). Create (); Window Window=alertdialog.getwindow ();/* Call the Setgravity method to make the dialog box appear at the top of the screen. */window.setgravity (Gravity.top); Alertdialog.show ();

tip :The parameters of thesetgravity method also support the following values:

Gravity.left (left), Gravity.right (right), Gravity.bottom (Bottom ),gravity.left| Gravity.top(upper left) and so on.

Run:



2. Display the dialog box in any location:

The Window object of the dialog has been acquired above, and we can use the object's getattributes () method to get the Windowmanager.layoutparams object, and through the Windowmanager.layoutparams object's windowmanager.layoutparams.x and Windowmanager.layoutparams The. Y property to set the position of the dialog box on the screen.

Alertdialog alertdialog=new Alertdialog.builder (this). Setmessage ("Show dialog Box Anywhere"). Setpositivebutton ("OK", null). Create (); Window Window=alertdialog.getwindow ();                    Windowmanager.layoutparams wl=window.getattributes ();//x,y offsets the dialog box's center position relative to the screen//sets the horizontal offset wl.x=-50;//set the vertical offset wl.y=100 ; Window.setattributes (WL); Alertdialog.show ();

tip : through windowmanager.layoutparams.x and windowmanager.layoutparams.y Sets the offset of the dialog box relative to the middle of the screen, not the absolute position of the screen.

Run:



3 Insert Picture degrees in the content text and buttons of the dialog box:

I have described inserting images in TextView in the TextView advanced app for Android development , and this technique works equally well in dialog boxes. The code for inserting a picture in the dialog box is as follows :

/** * Insert Picture degrees in the content text and buttons of the dialog box * */protected void Insertimg () {         //TODO auto-generated method stub         Alertdialog alertdial Og=new Alertdialog.builder (this).         setmessage (gethtml ()). Setpositivebutton (gethtml (), null). Create ();                   Alertdialog.show ();} Returns a spanned object private spanned gethtml () {         //TODO auto-generated method stub         spanned spanned=html.fromhtml (" Haha,  Hello.", new Imagegetter () {                   @Override public                   drawable getdrawable (String source) {                            // TODO auto-generated Method Stub                            drawable drawable=getresources (). getdrawable (R.drawable.ic_launcher);                            Set the thumbnail of the picture to small                            drawable.setbounds (0,0,30,30);                            return drawable;                   }         }, null);         return spanned;}

Run:



4. Change the transparency of the dialog box

You can set the transparency of the dialog box by WindowManager.LayoutParams.alpha . The Alpha value ranges from 0.0f to 1.0f (F for numbers of type float). 0.0f is completely transparent (at this point the dialog is not visible) and1.0f means completely opaque. 1.0f is also Alpha the default value . The following code shows a dialog box with an Alpha value of 0.6f.

/** * Change the transparency of the dialog box * */protected void Setalpha () {         //TODO auto-generated method stub         alertdialog alertdialog=new Ale Rtdialog.builder (This)         . Setmessage ("It's a transparent dialog box for 60%! "). Setpositivebutton (" OK ", null). Create ();         Window Window=alertdialog.getwindow ();         Window.setgravity (gravity.top);         Windowmanager.layoutparams wl=window.getattributes ();         The Alpha range is 0.0f-1.0f         wl.alpha=0.6f;         Window.setattributes (WL);         Alertdialog.show ();}

Run:


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.