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)