"Android" lets the Popupwindow appear above the view and align with the view's horizontal center

Source: Internet
Author: User

First of all, talk less, first on:



Code:

public class Mainactivity extends Activity implements onclicklistener{private button Showbtn1;private button showbtn2;@ overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main) showBtn1 = (Button) Findviewbyid (R.ID.SHOWBTN1); showBtn2 = (Button) Findviewbyid ( R.ID.SHOWBTN2); Showbtn1.setonclicklistener (this), Showbtn2.setonclicklistener (this);} @Overridepublic void OnClick (View v) {//TODO auto-generated method Stubview Popupview = Layoutinflater.from (this). Inflat E (r.layout.popup, NULL); Popupwindow Popupwindow = new Popupwindow (Popupview, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, True);p opupwindow.setbackgrounddrawable (New bitmapdrawable ()); Popupview.measure (measurespec.unspecified, measurespec.unspecified); int popupwidth = Popupview.getmeasuredwidth (); int popupheight = Popupview.getmeasuredheight (); int[] location = new Int[2];switch (V.getid ()) {case R.id.showbtn1:v. GetlocationoNscreen (location); Popupwindow.showatlocation (V, Gravity.no_gravity, (Location[0]+v.getwidth ()/2)-POPUPWIDTH/2, location[1]-    popupheight); break;case r.id.showbtn2:v.getlocationonscreen (location); Popupwindow.showatlocation (V, Gravity.no_gravity, (Location[0]+v.getwidth ()/2)-POPUPWIDTH/2, location[1]- Popupheight); break;default:break;}}}


Demo Download: http://download.csdn.net/detail/u011494050/7787359

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.