Popupwindow use
Popupwindow This class is used to implement a popup box that can use the view of any layout as its content , which is suspended above the current activity.
Popupwindow using Demo
The use of this class, no longer too much explanation, directly on the code bar.
such as the layout of the popup box:
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" match_parent " android:layout_height=" Wrap_content " android: Background= "#FFBBFFBB" android:orientation= "vertical" > <textview android:layout_width= "match _parent " android:layout_height=" wrap_content " android:padding=" 10DP " android:text=" Hello My Window " android:textsize= "20sp"/> <button android:id= "@+id/button1" android:layout_width= " Match_parent " android:layout_height=" wrap_content " android:padding=" 10DP " android:text=" button " android:textsize= "20SP"/></linearlayout>
There is only one button in the activity's layout, and a pop-up box is pressed and the activity code is as follows:
Package Com.example.hellopopupwindow;import Android.os.bundle;import Android.app.activity;import Android.content.context;import Android.util.log;import Android.view.layoutinflater;import Android.view.motionevent;import Android.view.view;import Android.view.view.onclicklistener;import Android.view.view.ontouchlistener;import Android.view.viewgroup.layoutparams;import Android.widget.Button;import Android.widget.popupwindow;import Android.widget.toast;public class Mainactivity extends Activity {private Context MCo ntext = null; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Mcontext = this; Button button = (button) Findviewbyid (R.id.button); Button.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (view view) { Showpopupwindow (view); } }); } private void ShowpopupwindoW (view view) {//a custom layout, as displayed content view Contentview = Layoutinflater.from (Mcontext). Inflate ( R.layout.pop_window, NULL); Set the button's Click event button button = (button) Contentview.findviewbyid (R.id.button1); Button.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) { Toast.maketext (Mcontext, "button is pressed", Toast.length_short). Show (); } }); Final Popupwindow Popupwindow = new Popupwindow (Contentview, Layoutparams.wrap_content, Layoutparams.wrap_c Ontent, True); Popupwindow.settouchable (TRUE); Popupwindow.settouchinterceptor (New Ontouchlistener () {@Override public boolean onTouch (View V, Mot Ionevent event) {log.i ("Mengdd", "OnTouch:"); return false; If this returns true, the touch event will be intercepted//intercepted after Popupwindow's ontouchevent is not called, so clickExternal zone cannot dismiss}}); If you do not set the background of the Popupwindow, either clicking the external area or the back button will not dismiss the frame//I think this is a bug in the API popupwindow.setbackgrounddrawable (get Resources (). getdrawable (R.drawable.selectmenu_bg_downward)); Set the parameters and then show Popupwindow.showasdropdown (view); }}
The pop-up box has a TextView and a Button,button click after the toast is displayed in the layout,
The first time the implementation of the problem encountered, that is, the popup box will not be pressed back when the time disappears, click the outside of the box is not normal disappear, search for a bit, all said as long as set the background is good.
Then I found a picture, and sure enough the frame can be normal dismiss (see note).
Popupwindow Source Code Analysis
To answer the above questions, look at the source code ( latest API level 19,android 4.4.2).
1. Display method
The display provides two forms:
Showatlocation () is displayed at the specified location with two method overloads:
public void Showatlocation (View parent, int gravity, int x, int. y) public void showatlocation (IBinder token, int gravity, I NT x, int y)
Showasdropdown () is displayed around a reference view with three method overloads:
public void Showasdropdown (view anchor) public void Showasdropdown (view anchor, int Xoff, int. yoff) public void Showasdropdo WN (View anchor, int xoff, int yoff, int gravity)
The last method with gravity parameters is the new one introduced by API 19.
The pop-up method first needs Preparepopup (), and finally Invokepopup ().
The Prepare method can be seen with or without background differences:
/** * <p>prepare the popup by embedding on into a new viewgroup if the * background drawable are NOT null. If embedding is required, the layout * parameters ' height was mnodified to take into account the background ' s * PA dding.</p> * * @param p The layout parameters of the popup ' s content view */private void Preparepopu P (Windowmanager.layoutparams p) {if (Mcontentview = = NULL | | mcontext = = NULL | | mwindowmanager = = NULL) { throw new IllegalStateException ("must specify a valid content view by" + "calling Setcontentvi EW () before attempting to show the Popup. "); if (mbackground! = null) {final Viewgroup.layoutparams layoutparams = Mcontentview.getlayoutparams (); int height = ViewGroup.LayoutParams.MATCH_PARENT; if (layoutparams! = null && layoutparams.height = = ViewGroup.LayoutParams.WRAP_CONTENT) { Height = ViEwGroup.LayoutParams.WRAP_CONTENT; }//When a background are available, we embed the content view//within another view that owns the Background drawable Popupviewcontainer Popupviewcontainer = new Popupviewcontainer (mcontext); Popupviewcontainer.layoutparams listparams = new Popupviewcontainer.layoutparams (viewgroup.layoutparam s.match_parent, height); Popupviewcontainer.setbackgrounddrawable (Mbackground); Popupviewcontainer.addview (Mcontentview, listparams); Mpopupview = Popupviewcontainer; } else {mpopupview = Mcontentview; } mpopupviewinitiallayoutdirectioninherited = (mpopupview.getrawlayoutdirection () = = View.layout_dir Ection_inherit); Mpopupwidth = P.width; Mpopupheight = P.height; }
2. Whether the background is empty for touch events
If there is a background, it will wrap a layer of Popupviewcontainer outside the Contentview as Mpopupview, and if there is no background, use Contentview as Mpopupview.
And this Popupviewcontainer is an internal private class that inherits the Framelayout, which overrides the distribution processing of key and touch events:
@Override public boolean dispatchkeyevent (KeyEvent event) {if (event.getkeycode () = = Keyevent.keycode_ back) {if (getkeydispatcherstate () = = null) {return super.dispatchkeyevent (event); } if (event.getaction () = = Keyevent.action_down && event.getrep Eatcount () = = 0) {keyevent.dispatcherstate state = Getkeydispatcherstate (); if (state = null) {state.starttracking (event, this); } return true; } else if (event.getaction () = = keyevent.action_up) {keyevent.dispatcherstate state = Getkeydispatcher State (); if (state! = NULL && state.istracking (event) &&!event.iscanceled ()) {dismiss (); return true; }} return SUPER.DIspatchkeyevent (event); } else {return super.dispatchkeyevent (event); }} @Override public boolean dispatchtouchevent (motionevent ev) {if (Mtouchinterceptor! = Null && Mtouchinterceptor.ontouch (this, Ev)) {return true; } return super.dispatchtouchevent (EV); } @Override public boolean ontouchevent (Motionevent event) {final int x = (int) event.getx (); Final int y = (int) event.gety (); if ((event.getaction () = = Motionevent.action_down) && ((x < 0) | | (x >= getwidth ()) | | (Y < 0) | | (Y >= getheight ()))) {Dismiss (); return true; } else if (event.getaction () = = Motionevent.action_outside) {dismiss (); return true; } else {return super.ontouchevent (event); }}
Since Popupview itself does not override the handling of key and touch events, if there is no package for this outer container class, clicking the back key or the outer area will not cause the box to disappear.
Supplemental case: Pop-up windows do not disappear, but events are passed down
Mentioned above:
Set the Popupwindow background, click the Back button or click the outside area of the pop-up window, the pop-up window will dismiss.
Conversely, if you do not set the Popupwindow background, then click the Back key and click the outside of the pop-up window, the pop-up window will not disappear.
So, if I want an effect, click on the outside area, the pop-up window does not disappear, but the click event will be passed to the following activity , for example, below is a webview, I want to click the link inside and so on.
Studied for half a day, said to set a flag for window,WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODALRead the source code, the flag is set or not by a field called Mnottouchmodal control, but the set method of setting the field is marked as @hide. So we're going to call through the reflection method:
/** * Set Whether this window is touch modal or if outside touches would be sent * to * Other windows behind it . * */public static void Setpopupwindowtouchmodal (Popupwindow Popupwindow, boolean touchmodal) { if ( NULL = = Popupwindow) { return; } Method method; try { method = PopupWindow.class.getDeclaredMethod ("Settouchmodal", boolean.class); Method.setaccessible (true); Method.invoke (Popupwindow, Touchmodal); } catch (Exception e) { e.printstacktrace (); } }
Then in the program:
Uiutils.setpopupwindowtouchmodal (Popupwindow, false);
Events outside the Popupwindow can be passed to the following activity.
Http://www.cnblogs.com/mengdd/p/3569127.html
Precautions:
1. Set background back key to close
Colordrawable DW = new colordrawable (0x00);
Mpopupwindow.setbackgrounddrawable (DW);
2, the code of a sentence
Popupwindow.setfocusable (TRUE);
Use and analysis of Android Popupwindow