Custom coordinate implementation of popupwindow in Android

Source: Internet
Author: User

In Android, the position of popupwindow is usually determined by the showasdropdown function. This function has two overload functions, which are defined as follows:

Public void showasdropdown (view anchor) {<br/> showasdropdown (anchor, 0, 0); <br/>}</P> <p> Public void showasdropdown (view anchor, int xoff, int yoff) {<br/> If (isshowing () | mcontentview = NULL) {<br/> return; <br/>}</P> <p> registerforscrollchanged (anchor, xoff, yoff); </P> <p> misshowing = true; <br/> misdropdown = true; </P> <p> windowmanager. layoutparams P = createpopuplayout (anchor. getwindowtoken (); <br/> preparepopup (p); </P> <p> updateaboveanchor (finddropdownposition (anchor, P, xoff, yoff )); </P> <p> If (mheightmode <0) p. height = mlastheight = mheightmode; <br/> If (mwidthmode <0) p. width = mlastwidth = mwidthmode; </P> <p> P. windowanimations = computeanimationresource (); </P> <p> invokepopup (p); <br/>}

That is to say, when the first function is called, the offset of the X and Y coordinates is 0 by default. In this case, the result of popupwindow is shown in the following figure. To implement popupwindow display at the bottom of Wenwen, the programmer needs to calculate the coordinate offset by himself. As shown in the lower right figure, When you click Wenwen, popupwindow is displayed at the bottom, this is exactly what we need. symmetry is a kind of beauty.




The key to code implementation is to click the response function after Wenwen. The code is directly used here:

Public void onclick (view v) {<br/> layoutinflater mlayoutinflater = (layoutinflater) getsystemservice (layout_inflater_service); <br/> viewgroup menuview = (viewgroup) mlayoutinflater. inflate (<br/> r. layout. tips, null, true); <br/> PW = new popupwindow (menuview, layoutparams. wrap_content, <br/> layoutparams. wrap_content, true); <br/> // click the return key to disappear without affecting the background. In this case, the setoutsidetouchable function is set to false even if it is set to <br/> // click popu. On the screen outside pwindow, popupwindow disappears. On the contrary, if backgrounddrawable is not set <br/> //, click the return key popupwindow, instant setoutsidetouchable is set to true <br/> // click the screen outside popupwindow, And the popupwindow will not disappear <br/> PW. setbackgrounddrawable (New colordrawable (color. transparent); <br/> PW. setoutsidetouchable (false); // you can specify whether to enable click to disappear. Is it useful? <Br/> PW. setanimationstyle (R. style. popupanimation); // set the animation </P> <p> // calculate the offset of the X axis to make the popupwindow appear at the bottom of the title, the unit here is pixels <br/> int xoffinpixels = screentools. getinstance (popdemoactivity. this ). getwidth ()/2-titlename. getwidth ()/2; <br/> // convert pixels to dip <br/> int xoffindip = screentools. getinstance (popdemoactivity. this ). px2dip (xoffinpixels); <br/> PW. showasdropdown (titlename,-xoffindip, 0); <br/> // PW. showasdropdown (titlename); <br/> PW. update (); </P> <p> textview TV = (textview) menuview. findviewbyid (R. id. tips_ OK); <br/> TV. setonclicklistener (new view. onclicklistener () {</P> <p> Public void onclick (view v) {<br/> PW. dismiss (); <br/>}</P> <p >}); </P> <p>}

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.