The perfect implementation of the Popupwindow pop-up window (for a popup background dimming effect)

Source: Internet
Author: User

Recently tried to use Popupwindow to achieve the background dimming effect, self-optimization, and encapsulated into a method that can be called, the default implementation of the pop-up window appears under the incoming view, the following code has a detailed comment, there are questions can leave a message

The results are shown below:

/** * I encapsulated this popupwindow method, * The first parameter is which control he wants to display under * The second parameter is the layout file ID to be populated into Popupwindow * The third parameter is to give the Popupwindow a Background Resource ID * /      Private void Showpopwindow(View V,intConvertviewresource,intDrawbelresource) {//Create a Popupwindow        if(Popupwindow = =NULL{View poplayout = Layoutinflater.from (Getactivity ()). Inflate (Convertviewresource,NULL);//Set the Click event to the space within the PopupwindowPoplayout.findviewbyid (R.id.tv_pop_notice). Setonclicklistener (NewView.onclicklistener () {@Override                 Public void OnClick(View v) {Intent Intent =NewIntent (Getactivity (), findguanzhuactivity.class);                StartActivity (Intent);            }            }); Poplayout.findviewbyid (R.id.tv_pop_group). Setonclicklistener (NewView.onclicklistener () {@Override                 Public void OnClick(View v) {Intent Intent =NewIntent (Getactivity (), groupfindactivity.class);                StartActivity (Intent); }            });//instantiation of a PopupwindowPopupwindow =NewPopupwindow (Poplayout, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT,true);//Create background dimming effectWindowmanager.layoutparams LP = Getactivity (). GetWindow (). GetAttributes (); Lp.alpha =0.4F Getactivity (). GetWindow (). SetAttributes (LP);//Click outside Popupwindow to disappearPopupwindow.setoutsidetouchable (true);//popupwindow Get FocusPopupwindow.setfocusable (true);//popupwindow Setting the background mapdrawable drawable = Getresources (). getdrawable (Drawbelresource); Popupwindow.setbackgrounddrawable (drawable);//popupwindow set opening animation style            //popupwindow.setanimationstyle (R.style.popupwindow_anim);            //Refresh Popupwindow            //popupwindow.update ();            //Set the Popupwindow when the monitor disappearsPopupwindow.setondismisslistener (NewPopupwindow.ondismisslistener () {//restore transparency in dismiss                 Public void Ondismiss() {Windowmanager.layoutparams LP = Getactivity (). GetWindow (). GetAttributes (); Lp.alpha =1F                Getactivity (). GetWindow (). SetAttributes (LP);            }            });        Popupwindow.showasdropdown (v); }Else{//If Popupwindow is being displayed, then hide            if(Popupwindow.isshowing ())            {Popupwindow.dismiss (); }Else{//Create background dimming effectWindowmanager.layoutparams LP = Getactivity (). GetWindow (). GetAttributes (); Lp.alpha =0.4F                Getactivity (). GetWindow (). SetAttributes (LP);            Popupwindow.showasdropdown (v); }        }       `}

The perfect implementation of the Popupwindow pop-up window (for a popup background dimming effect)

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.