Popupwindow Adaptive width layout in Android development

Source: Internet
Author: User

Popupwindow Usage

Use Popupwindow to achieve pop-up window effect, in fact, and alertdialog like, is also a dialog box, both often mixed, but also have their own characteristics. 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 layout
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 set the Popupwindow size manually.
Mpop.setcontentview (Menuview);/set include view
Mpop.setwidth (int)
Mpop.setheight (int)/Set pop-up box size

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


Mpop.setoutsidetouchable (TRUE);//This sets whether the outside clicks are valid after the Popuwindow is displayed. If False, then clicking outside the Popuwindow does not close the Popuwindow. Of course it is obvious that it can only be used under touchable.

When there is a mpop.setfocusable (false), the Popuwindow can not get focus, even if the settings set the background is not empty and can not be clicked to disappear, can only be disappeared by dismiss (), but the outside view of the event can still trigger, The back key can also smooth dismiss off. When set to Popuwindow.setfocusable (true), add the following two lines to set the background code and click the outside and back keys to disappear.
Mpop.setfocusable (TRUE);
You need to make the Popupwindow Dimiss (that is, if you click outside the popuwindow where this or the back key Popuwindow disappears); Popupwindow background cannot be empty. The background that must be set before Popuwindow.showasdropdown (v) or another display Popuwindow method is not empty:

Mpop.setbackgrounddrawable (new colordrawable (0));



Mpop.showasdropdown (anchor, 0, 0);//Set the position to display Popupwindow is located at the lower left of the view, x,y represents the coordinate offset

Mpop.showatlocation (Findviewbyid (r.id.parent), Gravity.left, 0,-90); (Refer to a view for reference), indicating that the pop-up window is referenced by the parent component, on the left side, offset-90.
Mpop.setondismisslistenerd (New Popupwindow.ondismisslistener () {})/Set Window vanishing event

Note: Window.xml is a layout file

Summarize:

1, for the Popupwindow view layout, through the Layoutinflator to obtain layout view.

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 animation

Pop.setanimationstyle (r.style.popupanimation);



4, click Popupwindow Area outside, 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;

}

});



Popupwindow Adaptive Width Example


@Override
protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {
int maxwidth = Meathurewidthbychilds () + getpaddingleft () + getpaddingright (); Super.onmeasure (Measurespec.makemeasurespec (maxwidth,measurespec.exactly), heightmeasurespec);
}
public int Meathurewidthbychilds () {
int maxwidth = 0;
View view = null;
for (int i = 0; i < Getadapter (). GetCount (); i++) {
view = Getadapter (). GetView (I, view, this);
View.measure (measurespec.unspecified, measurespec.unspecified);
if (View.getmeasuredwidth () > MaxWidth) {
MaxWidth = View.getmeasuredwidth ();
}
}
return maxwidth;
}



Popupwindow Adaptive Layout

Android's own menu, often unable to meet our needs, so only write the menu itself, the usual choice is to use Popupwindow to implement the custom menu menu, first look at the code, and then explain the points to note:

View Menuview = inflater.inflate (R.layout.menu_popwindow, NULL);
Final Popupwindow p = new Popupwindow (mcontext);
P.setcontentview (Menuview);
P.setwidth (ViewGroup.LayoutParams.FILL_PARENT);
P.setheight (ViewGroup.LayoutParams.WRAP_CONTENT);
P.setanimationstyle (R.style.menuwindow);
P.setondismisslistener (this);
P.setoutsidetouchable (FALSE);
P.setbackgrounddrawable (New Colordrawable, Android. R.color.transparent));
P.setfocusable (TRUE); If you set the focus to false, the other parts are clickable, which means that when you pass the event, you don't go first Popupwindow

Mpopwindow = p;

To illustrate some of these points:

1. In order to allow Popupwindow adaptive screen width, set width with ViewGroup.LayoutParams.FILL_PARENT, in order to adaptively the height of the child layout, Use ViewGroup.LayoutParams.WRAP_CONTENT when setting height

2. Since the Popupwindow class does not inherit the ViewGroup class, the second parameter of the inflater.inflate (int resource, ViewGroup root) method can only be passed null. Passing null will not affect the outermost layout of the android:layout_xxx. So the height is based on the second floor layout.

3. In order to set the background and margin, its background can only be set in the second layout, because the first level of the layout of the android:layout_marginxxx does not work, and the setting android:padding_xxx does not affect the background.

  4. Menu has a feature, is the point outside, the menu to disappear, to achieve this, there are several properties to set together: P.setoutsidetouchable (False);p. setbackgrounddrawable (); P.setfocusable (TRUE);

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.