In the actual development, we may use the toolbar control, and then add the pop-up menu in the upper right corner, but toolbar popup menu, it is not very good control position, so the user experience is not good, so I think of using Popupwindow instead of pop-up menu. Don't say a few words first look at the effect (the end of the article is demo):
The effect is basically acceptable.
Let's talk about the implementation process:
A: Menu layout, very simple, and the general layout, the LinearLayout package TextView and ImageView (need pictures)
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Wrap_content "android:layout_height=" wrap_content "android:gravity=" Center_horizon Tal "android:background=" @drawable/icon_popupwin_menu_bg "android:orientation=" vertical "> <linearlayout Android:id= "@+id/ly_item1" android:layout_width= "match_parent" android:layout_height= "45dip" and Roid:gravity= "center_vertical" android:orientation= "horizontal" android:background= "@drawable/bg_default_bu Tton_selector "> <textview android:layout_width=" wrap_content "android:layout_height=" WR Ap_content "android:textsize=" 15sp "android:paddingleft=" 20dip "android:textcolor=" #595d5 F "android:text=" tab One "/> </LinearLayout> <view android:layout_width=" Match_pa Rent "android:layout_height="1dip" android:background= "#eeeeee"/> <linearlayout android:id= "@+id/ly_item2" Android:layo Ut_width= "Match_parent" android:layout_height= "45dip" android:gravity= "center_vertical" Android:orien tation= "Horizontal" android:background= "@drawable/bg_default_button_selector" > <imageview Android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:paddingleft= "20dip" android:paddingright= "20dip" android:visibility= "Gone"/> <textview Android : layout_width= "wrap_content" android:layout_height= "wrap_content" android:textsize= "15SP" android:paddingleft= "20dip" android:textcolor= "#595d5f" android:text= "tab two"/> </linearlay out> <view android:layout_width= "match_parent" android:layout_height= "1dip" Android:backgrou nd= "#eeeeee"/> < LinearLayout android:id= "@+id/ly_item3" android:layout_width= "Match_parent" android:layout_height= "45 Dip "android:gravity=" center_vertical "android:orientation=" horizontal "android:background=" @drawable /bg_default_button_selector "> <imageview android:layout_width=" wrap_content "android:l ayout_height= "Wrap_content" android:paddingleft= "20dip" android:paddingright= "20dip" Android oid:visibility= "Gone"/> <textview android:layout_width= "Wrap_content" Android:layout_h eight= "Wrap_content" android:textsize= "15sp" android:paddingleft= "20dip" Android:textcolo R= "#595d5f" android:text= "tab three"/> </LinearLayout></LinearLayout>
Here with the title bar and click on the color effect, this is not the focus of this, so do not say, do not understand in the blog last download demo to see, hh~
II: Inherit Popupwindow to implement your own menu
The main is to add layout files, set layout file location, no option to add click Callback interface, the code is as follows, comments are also detailed:
Package Com.ywl5320.popupmenu;import Android.app.activity;import Android.content.context;import Android.graphics.drawable.colordrawable;import Android.view.layoutinflater;import Android.view.View;import Android.view.view.onclicklistener;import Android.widget.linearlayout.layoutparams;import Android.widget.popupwindow;public class PopupMenu extends Popupwindow implements Onclicklistener {private Activity Activity;private View Popview;private View V_item1;private view V_item2;private View V_item3;private Onitemclicklistener onitemclicklistener;/** * * @author YWL5320 enumeration to distinguish which option is selected */public enum MENUITEM {ITEM1, ITEM2, ITEM 3}public PopupMenu (activity activity) {super (activity); this.activity = activity; Layoutinflater Inflater = (layoutinflater) activity.getsystemservice (context.layout_inflater_service);p Opview = Inflater.inflate (R.layout.popup_menu, NULL);//Load Menu layout file This.setcontentview (popview);// Add the layout file to Popupwindow this.setwidth (dip2px (activity, 120));//Set the width of the menu (need to match the menu to the right margin, you can adjust to the appropriateThis.setheight (layoutparams.wrap_content); this.setfocusable (true);//Get Focus this.settouchable (TRUE); Set Popupwindow to Touch this.setoutsidetouchable (true); Set the non-Popupwindow area to touch colordrawable DW = new colordrawable (0x00000000); this.setbackgrounddrawable (DW);//Get Tab V_ Item1 = Popview.findviewbyid (r.id.ly_item1); v_item2 = Popview.findviewbyid (r.id.ly_item2); v_item3 = Popview.findviewbyid (R.ID.LY_ITEM3);//Add Listener v_item1.setonclicklistener (this); V_item2.setonclicklistener (this); v_ Item3.setonclicklistener (this);} /** * Set the location of the display * * @param resourid * Here the X, Y value itself adjusts can be */public void showlocation (int resourid) {Showasdropdown (act Ivity.findviewbyid (Resourid), dip2px (activity, 0), dip2px (activity,-8));} @Overridepublic void OnClick (View v) {//TODO auto-generated method Stubmenuitem menuitem = null; String str = ""; if (v = = v_item1) {MenuItem = MenuItem. ITEM1;STR = "tab One";} else if (v = = v_item2) {MenuItem = MenuItem. ITEM2;STR = "tab Two";} else if (v = = v_item3) {MenuItem = MenuItem. item3;str = "optionKasan ";} if (Onitemclicklistener! = null) {Onitemclicklistener.onclick (MenuItem, str);} Dismiss ();} Dip conversion to Pxpublic int dip2px (context context, float Dipvalue) {final float scale = context.getresources (). Getdisplaymetri CS (). Density;return (int) (Dipvalue * scale + 0.5f);} Click on the Listener interface public interface Onitemclicklistener {public void OnClick (MENUITEM item, String str);} Set listener public void Setonitemclicklistener (Onitemclicklistener onitemclicklistener) {This.onitemclicklistener = Onitemclicklistener;}}
Three: Then call in mainactivity:
Initialize the menu, then set the menu location, add the callback to OK
Package Com.ywl5320.popupmenu;import Android.app.activity;import Android.os.bundle;import android.view.View;import Android.view.view.onclicklistener;import Android.widget.imageview;import Android.widget.toast;import Com.ywl5320.popupmenu.popupmenu.menuitem;import Com.ywl5320.popupmenu.popupmenu.onitemclicklistener;public Class Mainactivity extends Activity {private ImageView iv_menu;private popupmenu popupmenu; @Overrideprotected void OnCreate ( Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main);p Opupmenu = New PopupMenu (this); Iv_menu = (ImageView) Findviewbyid (R.id.iv_menu); Iv_menu.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {//TODO auto-generated method Stubpopupmenu.showlocation (R.id.iv_menu);// Set pop-up menu popup location//Set callback listener, get click event Popupmenu.setonitemclicklistener (New Onitemclicklistener () {@Overridepublic void OnClick (MENUITEM item, String str) {//TODO auto-generated method Stubtoast.maketext (mainactivity.this, str, 0). Show ();}});}});}} Let's look at the layout of the title, and it's simple:
<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Match_parent "android:layout_height=" 55dip "android:background=" #262a3b "> <imageview android:id= "@+id/iv_back" android:layout_width= "50dip" android:layout_height= "match_p Arent "android:layout_alignparentleft=" true "android:layout_centervertical=" true "android:scaletype=" Centerinside "android:visibility=" invisible "/> <textview android:id=" @+id/tv_title "and Roid:layout_width= "Match_parent" android:layout_height= "Match_parent" android:singleline= "true" Andro id:layout_toleftof= "@+id/iv_menu" android:layout_torightof= "@+id/iv_back" android:gravity= "center" an droid:text= "title bar" android:textcolor= "#ffffff" android:textsize= "22SP" android:layout_centerinparent= "t Rue "/> <imageview Android:id= "@+id/iv_menu" android:layout_width= "50dip" android:layout_height= "Match_parent" Android: Layout_alignparentright= "true" android:layout_centervertical= "true" android:scaletype= "Centerinside" android:src= "@drawable/menu_icon"/></relativelayout>
This realization of the top right pop-up menu, in fact, not only in the upper right corner, can be any place pop-up menu, like that can also, well, it is here:Demo Download
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android Pop-up menu