Android:popupwindow usage Scenarios and considerations

Source: Internet
Author: User

Features of 1.PopupWindow

Borrowing Google's official statement:

A Popup window that can is used to the display an arbitrary view. The popup window is a floating container this appears on topof the current activity. "

In other words, Popupwindow is a suspended container above the activity, which can display arbitrary views of the view, very domineering appearance. Let's take a look at how it's used.

2. Initialize some features of Popupwindow

Example:

Popupwindow Popupwindow = new Popupwindow (Getapplicationcontext ());p Opupwindow.setcontentview (ContentView);// You can set any viewpopupwindow.setwidth (layoutparams.wrap_content);//Set the width popupwindow.setheight (layoutparams.wrap_ CONTENT);//Height Popupwindow.setanimationstyle (r.anim.abc_fade_in);//Display animation popupwindow.setfocusable (TRUE);// Set whether to get focus

Where Contentview is the view you want to display.

3.PopupWindow Display and hide

The method shown:

public void Showatlocation (View parent, int gravity, int x, int. y) Added in API level 1Display the content View in a popup Window at the specified location. If the popup window cannot fit on screen, it'll be clipped. See Windowmanager.layoutparams For more information in how gravity and the x and Y parameters is related. Specifying a gravity of no_gravity is similar to specifying Gravity.left | Gravity.TOP.Parametersparenta Parent view to get the Getwindowtoken () token fromgravitythe Gravity which controls the Plac Ement of the Popup windowxthe popup ' s x location offsetythe popup's Y location offset

Popupwindow. showatlocation (Contentview, Gravity. CENTER , 0, 0) ;//Set Center

Popupwindow. showatlocation (Contentview, Gravity. no_gravity , x , y) ;//Display the position of the window with (x, y) as the upper-left corner


Hide:

if (Popupwindow! = null&& popupwindow.isshowing ()) {Popupwindow.dismiss ();p Opupwindow = null;}


Related: Note that when calculating the location of the view:

Android provides some ways to get a view in the screen position.
1). Getlocationonscreen, calculates the x, Y value of the view in the global coordinate system, gets the absolute coordinates within the current screen (the value is counted from the top of the screen, including the height of the notification bar).
2). Getlocationinwindow, calculates the x, Y value of the view in the coordinates of the widnow where it resides.
3) GetLeft, GetTop, Getbottom, GetRight, this group is to get the coordinates relative to its father's layout.


Related: Popupwindow animation: http://blog.csdn.net/wl455624651/article/details/7798879

more communication, Android Development Alliance QQ Group: 272209595



Android:popupwindow usage Scenarios and considerations

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.