Today, I figured out how to use popuwindow in Android, and lost my head. popuwindow was always unavailable or displayed. Later I found out some information on the Internet.
Later I summarized some lessons
1. The size of popuwindow is controlled by the following code;
Newpopupwindow (view, viewgroup. layoutparams. fill_parent,
Viewgroup. layoutparams. wrap_content );
2. popuwindow. showasdropdown (V); The popuwindow is displayed in the lower left of view V;
3. The popupwindow dimiss needs to be smoothly enabled (that is, the position other than popuwindow or the back key popuwindow will disappear); The popupwindow background cannot be blank. The background of popuwindow. showasdropdown (v) must be set before popuwindow. showasdropdown (V); or other popuwindow display methods are not empty. For example, the following two lines of code:
Colordrawablecd = new colordrawable (-0000 );
Popuwindow. setbackgrounddrawable (CD );
Popuwindow. showasdropdown (v );
Note that setting the background does not overwrite the background defined in the XML file.
4. When popuwindow exists. when setfocusable (false); is set, it indicates that popuwindow cannot obtain the focus. Even if the Set background is not empty, it cannot be clicked to disappear, but dismiss () can only disappear, however, the view event outside can still be triggered, and the back key can be smoothly dismiss. When it is set to popuwindow. setfocusable (true);, add the following two lines of background code, and click the outside and back key to disappear.
5. // here, it is set to show whether popuwindow is valid after clicking outside. If it is false, clicking the outside of popuwindow will not close the popuwindow. Of course, it is obvious that only touchable can be used.
Popuwindow. setoutsidetouchable (true );
The following is a simple example of using popuwindow:
Public class mypopuwindowactivity2 extends activity {popupwindow popuwindow; imageview imgcall; private view; private button btnemail; private button btnsms; private button btncall; protected void oncreate (Android. OS. bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. childlayout); imgcall = (imageview) findviewbyid (R. id. mycursor); imgcall. setonclicklistener (New popu Onclicklistener (); initpopupwindow () ;};/*** this class mainly displays popuwindow and then adds listening events to the buttons. */Private class popuonclicklistener implements onclicklistener {@ overridepublic void onclick (view v) {system. out. println ("onclick mycursor"); Switch (v. GETID () {case R. id. mycursor: // you can use the back key to close the popupwindow with the following two rows. Otherwise, you must call dismiss (). // You must smoothly set the popupwindow dimiss. The background of popupwindow cannot be blank. // When popuwindow. setfocusable (false); exists, it indicates that popuwindow cannot obtain the focus, and it cannot be clicked to disappear, but dismiss () can only disappear. // When it is set to popuwindow. setfocusable (true);, the following two lines of code will disappear. // note that adding a background here will not overwrite the original background. Colordrawable Cd = new colordrawable (-0000); popuwindow. setbackgrounddrawable (CD); popuwindow. showasdropdown (V); btnsms. setonclicklistener (new view. onclicklistener () {@ overridepublic void onclick (view v) {popuwindow. dismiss (); Uri uri = Uri. parse ("Maid: 123456789"); intent it = new intent (intent. action_sendto, Uri); it. putextra ("sms_body", "haha! Long time no see "); startactivity (IT) ;}}); btnemail. setonclicklistener (new view. onclicklistener () {@ overridepublic void onclick (view v) {popuwindow. dismiss (); Uri uri = Uri. parse ("mailto: 674639577@qq.com"); intent it = new intent (intent. action_sendto, Uri); startactivity (IT) ;}}); btncall. setonclicklistener (new view. onclicklistener () {@ overridepublic void onclick (view v) {popuwindow. dismiss (); Uri uri = Uri. parse ("Tel: 15873155401 "); intent it = new intent (intent. action_dial, Uri); startactivity (IT) ;}}); break; default: Break ;}} private void initpopupwindow () {view = This. getlayoutinflater (). inflate (R. layout. popup_window, null); popuwindow = new popupwindow (view, viewgroup. layoutparams. fill_parent, viewgroup. layoutparams. wrap_content); // set whether to display popuwindow and click whether it is valid. If it is false, clicking the outside of popuwindow will not close the popuwindow. Popuwindow. setoutsidetouchable (true); // you cannot use btnsms = (button) view without focus. findviewbyid (R. id. btnsms); btnemail = (button) view. findviewbyid (R. id. btnemail); btncall = (button) view. findviewbyid (R. id. btncall) ;}@ overrideprotected void ondestroy () {// todo auto-generated method stubsuper. ondestroy (); If (popuwindow. isshowing () {popuwindow. dismiss ();}}}