Use of Popupwindow

Source: Internet
Author: User


Popupwindow Popupwindow = new Popupwindow (this);
Popupwindow.setcontentview (Layoutinflater.from (this). Inflate (R.layout.activity_main, null));
Popupwindow.setwidth (110);
Popupwindow.setheight (500);
Popupwindow.showatlocation (Viewpager, gravity.bottom, 0, 0);

Can

The use of Popupwindow can achieve pop-up effect, in fact, like Alertdialog, is also a dialog box, both are often mixed, but also each has a feature. Here's how to use it.
First Initializes a Popupwindow, specifying the window size parameter.


Popupwindow mPop =new Popupwindow (Getlayoutinflater (). Inflate (R.layout.window,null),
Layoutparams.wrap_content,layoutparams.wrap_content);
You can also write separately:
Layoutinflater Mlayoutinflater = (layoutinflater) getsystemservice (Layout_inflater_service);
Custom Layouts
ViewGroup Menuview = (viewgroup) mlayoutinflater.inflate (
R.layout.window, NULL, TRUE);
Popupwindow mpop= New Popupwindow (Menuview,layoutparams.wrap_content,
Layoutparams.wrap_content, True);
Of course, you can also manually set the Popupwindow size.
Mpop.setcontentview (Menuview);//set include view
Mpop.setwidth (int)
Mpop.setheight (int)//Set PopOver size

Set up an entry animation:
Mpop.setanimationstyle (R.style.animationpreview);//Set animation style

Mpop.setoutsidetouchable (TRUE);//settings shown herePopuwindowthen click outside to see if it works. If thefalsewords, then clickPopuwindowIt's not closed outside .Popuwindow. Of course, it's obviously only intouchableto use.

When there is mpop.setfocusable (false); the time, the description Popuwindow cannot get focus, even if setting the background is not empty and cannot click outside to disappear, only by dismiss () disappeared, but the outside View the event can still be triggered , back keys can also be smooth Dismiss off. When set to popuwindow.setfocusable (true); , Add the following two lines to set the background code, click outside and Back key will not disappear.

Mpop.setfocusable (TRUE);
Need to smoothly letPopupwindow Dimiss(that is, clickPopuwindowoutside of the place this or BackKeyPopuwindowwill disappear);Popupwindowthe background cannot be empty. Must be inPopuwindow.showasdropdown (v);or any other display.Popuwindowthe background of the method before setting it is not empty:

Mpop.setbackgrounddrawable (newcolordrawable (0));



Mpop.showasdropdown (anchor, 0,0);//settings display the location of the Popupwindow at the lower left of the view, X, y represents the coordinate offset

Mpop.showatlocation (Findviewbyid (r.id.parent), Gravity.left, 0,-90); (as a reference to a view), indicating that the pop-up window is referenced to the parent component, on the left, and offset-90.

Mpop.setondismisslistenerd (Newpopupwindow.ondismisslistener () {})//Set window vanishing event

Note: Window.xml is a layout file


Summarize:

1, for the Popupwindow View layout, through the layoutinflator to get the layout of the View. such as :

Layoutinflater Inflater = (layoutinflater)

This.anchor.getContext (). Getsystemservice (Context.layout_inflater_service);

View Textentryview = inflater.inflate (R.layout.paopao_alert_dialog, NULL);

2, display position, there are many ways to set the display mode

Pop.showatlocation (Findviewbyid (R.ID.LL2), Gravity.left, 0,-90);

Or

Pop.showasdropdown (View anchor, int xoff, Intyoff)

3, into the appearance of the animation

Pop.setanimationstyle (r.style.popupanimation);

4, click outside the Popupwindow area, Popupwindow disappear

This.window = new Popupwindow (Anchor.getcontext ());

This.window.setTouchInterceptor (New Ontouchlistener () {

@Override

public boolean OnTouch (View V, motionevent event) {

if (Event.getaction () ==motionevent.action_outside) {

BetterPopupWindow.this.window.dismiss ();

return true;

}

return false;

}

});

Use of Popupwindow

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.