Android Development 64:popupwindow Implementation of custom popup menus

Source: Internet
Author: User

Open the Popupwindow source code, you will find that it is also through the WindowManager to add view.

       private void Invokepopup (Windowmanager.layoutparams p) {        if (mcontext! = null) {            P.packagename = Mcontext.getpackagename ();        }        Mpopupview.setfitssystemwindows (Mlayoutinsetdecor);        Setlayoutdirectionfromanchor ();        Mwindowmanager.addview (Mpopupview, p);       }

The effect of this article is to pop a custom menu through popupwindow :


Click on the menu in the top right corner, pop-up menu items, OK, let's achieve this effect.

The first step, the Design menu page Layout_menu.xml

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" 156DP "android:layout_height=" 150DP "android:background=" #9AFF9A "Android:orien tation= "vertical" > <linearlayout android:id= "@+id/layout_share" android:layout_width= "match_parent        "Android:layout_height=" 49DP "android:background=" @drawable/item_bg_style "android:clickable=" true "            android:orientation= "Horizontal" > <imageview android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:layout_gravity= "center_vertical" Android:layout_margi nleft= "20DP" android:layout_marginright= "20DP" android:src= "@drawable/ic_share"/> <tex TView android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:la     Yout_gravity= "Center_vertical"       android:text= "@string/lbl_share" android:textcolor= "#187D18" > </TextView> </linea rlayout> <linearlayout android:layout_width= "match_parent" android:layout_height= "1DP" Andro id:layout_marginleft= "10DP" android:layout_marginright= "10DP" android:background= "#87EC87" > </line        arlayout> <linearlayout android:id= "@+id/layout_setting" android:layout_width= "Match_parent" android:layout_height= "49DP" android:background= "@drawable/item_bg_style" android:clickable= "true" a ndroid:orientation= "Horizontal" > <imageview android:layout_width= "wrap_content" Androi d:layout_height= "Wrap_content" android:layout_gravity= "center_vertical" android:layout_marginleft= "2            0DP "android:layout_marginright=" 20DP "android:src=" @drawable/ic_setting "/> <textview Android:layouT_width= "Wrap_content" android:layout_height= "wrap_content" android:layout_gravity= "center_vertical" android:text= "@string/lbl_setting" android:textcolor= "#187D18" > </TextView> &lt        ;/linearlayout> <linearlayout android:layout_width= "match_parent" android:layout_height= "1DP" android:layout_marginleft= "10DP" android:layout_marginright= "10DP" android:background= "#87EC87" > &L        t;/linearlayout> <linearlayout android:id= "@+id/layout_update" android:layout_width= "Match_parent"        android:layout_height= "49DP" android:background= "@drawable/item_bg_style" android:clickable= "true"            android:orientation= "Horizontal" > <imageview android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:layout_gravity= "center_vertical" Android:layout_marginl Eft= "20DP" android:layout_marginright= "20DP" android:src= "@drawable/ic_update"/> <textview android: Layout_width= "Wrap_content" android:layout_height= "wrap_content" android:layout_gravity= "Center_vert    ical "android:text=" @string/lbl_update "android:textcolor=" #187D18 "> </TextView> </LinearLayout></LinearLayout>

second step, pop-up menu

 View Popview = Getlayoutinflater (). Inflate (R.layout.layout_menu, NULL);        LinearLayout Layoutshare = (linearlayout) Popview.findviewbyid (R.id.layout_share);                Layoutshare.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {        TODO auto-generated Method stub}});        LinearLayout layoutsetting = (linearlayout) Popview.findviewbyid (r.id.layout_setting);                Layoutsetting.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {                StartActivity (New Intent (Mainactivity.this, Settingactivity.class));                if (popup!=null&&popup.isshowing ()) {Popup.dismiss ();        }            }        });        LinearLayout layoutupdate = (linearlayout) Popview.findviewbyid (r.id.layout_update); Layoutupdate.setonclicklistener (New Onclicklistener () {@Override public voiD OnClick (View v) {//TODO auto-generated Method stub}}); Pobupwindow popup = new Popupwindow (Popview, uiutils.dip2px (Mainactivity.this, 156), uiutils.dip2px (MainActivity.this        , 150));        Popup.setfocusable (TRUE);        Popup.setoutsidetouchable (TRUE);        Popup.setbackgrounddrawable (New bitmapdrawable ());        Layoutmenucontainer = (linearlayout) Findviewbyid (R.id.layout_menu);  Layoutmenucontainer.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v)                    {try {int[] location = new INT[2];                    V.getlocationonscreen (location);                    if (popup.isshowing ()) {Popup.dismiss ();                        } else {//Popup.showasdropdown (v);                        Popup.showasdropdown (V,-uiutils.dip2px (Mainactivity.this, 129), 0); Popup.showatloCation (V,//gravity.no_gravity,location[0]-uiutils.dip2px (mainactivity.this,100), Location[1]+uiuti                    LS.DIP2PX (mainactivity.this,60));                    }} catch (Exception e) {if (E! = null) {e.printstacktrace (); }                }            }        });


Step Three, Androidmanifest.xml configure permissions

<uses-permission android:name= "Android.permission.SYSTEM_ALERT_WINDOW"/>



Android Development 64:popupwindow Implementation of custom popup menus

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.