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.
New NULL ), layoutparams.wrap_content, layoutparams.wrap_content);
You can also write separately:
Layoutinflater mlayoutinflater = (layoutinflater) Getsystemservice (layout_inflater_service); // Custom Layouts ViewGroup Menuview =nulltruenewtrue);
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); // here set the display Popuwindow after the click on the outside is valid. If False, then clicking outside of Popuwindow does not turn off Popuwindow. Of course it is obvious that it can only be used under touchable.
When there is mpop.setfocusable (false), the Popuwindow cannot get the focus, even if the settings set the background is not empty and can not click the outside disappear, only by dismiss () disappear, but the outside view of the event can be triggered, The back key can also be dismiss off smoothly. When set to Popuwindow.setfocusable (true), add the following two lines to set the background code, click the outside and the back button will disappear.
Mpop.setfocusable (TRUE);
Need to smooth Popupwindow Dimiss (i.e. click outside the Popuwindow or back key Popuwindow will disappear); Popupwindow background cannot be empty. The background of the Popuwindow.showasdropdown (v) must be set before the Popuwindow method is displayed, or it is not empty:
0 0); // sets the position of the display Popupwindow at the lower left of the view, X, y for coordinate offsets 0,-+); (as a reference to a view), indicating that the pop-up window is referenced by the parent component, on the left side, offset--. Mpop.setondismisslistenerd (new Popupwindow.ondismisslistener () {})// Set window vanishing event Note: Window.xml is a layout file
Summarize:
1. For Popupwindow view layout, get the layout view through Layoutinflator.
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, int yoff)
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;
}
});
Introduction to the usage properties of Android Showasdropdown