Popwindow is similar to the menu in the upper right corner, and popwindow is similar to the menu in the upper right corner.
MenuPopwindow:
1 package com. cloudeye. android. cloudeye. view; 2 3 import android. app. activity; 4 import android. content. context; 5 import android. graphics. drawable. colorDrawable; 6 import android. view. layoutInflater; 7 import android. view. view; 8 import android. view. viewGroup; 9 import android. widget. adapterView; 10 import android. widget. baseAdapter; 11 import android. widget. imageView; 12 import android. widget. lis TView; 13 import android. widget. popupWindow; 14 import android. widget. textView; 15 16 import com. cloudeye. android. cloudeye. r; 17 import com. cloudeye. android. cloudeye. base. menuPopwindowBean; 18 19 import java. util. list; 20 21/** 22 * Created by Yuan lei on 2016/10/26. 23 */24 public class MenuPopwindow extends PopupWindow {25 private View conentView; 26 private ListView lvContent; 27 28 public MenuPop Window (Activity context, List <MenuPopwindowBean> list) {29 LayoutInflater inflater = (LayoutInflater) context 30. getSystemService (Context. LAYOUT_INFLATER_SERVICE); 31 conentView = inflater. inflate (R. layout. menu_popup_window, null); 32 lvContent = (ListView) conentView. findViewById (R. id. lv_toptitle_menu); 33 lvContent. setAdapter (new MyAdapter (context, list); 34 int h = context. getWindowManager (). Getdefadisplay display (). getHeight (); 35 int w = context. getWindowManager (). getdefadisplay display (). getWidth (); 36 // set the View 37 this of SelectPicPopupWindow. setContentView (conentView); 38 // set the width of the SelectPicPopupWindow pop-up window to 39 this. setWidth (w/3-30); 40 // set the height of the SelectPicPopupWindow pop-up window to 41 this. setHeight (ViewGroup. layoutParams. WRAP_CONTENT); 42 // set the SelectPicPopupWindow pop-up window. You can click 43 this. setFocusable (true); 44 this. SetOutsideTouchable (true); 45 // refresh status 46 this. update (); 47 // instantiate a ColorDrawable color to translucent 48 ColorDrawable dw = new ColorDrawable (0000000000); 49 // point back key and other places to make it disappear, if this parameter is set, OnDismisslistener can be triggered, and other operations such as other control changes can be set to 50 this. setBackgroundDrawable (dw); 51 // mPopupWindow. setAnimationStyle (android. r. style. animation_Dialog); 52 // set SelectPicPopupWindow to bring up the form animation effect 53 this. setAnimationStyle (R. style. animationPreview); 5 4} 55 56 57 public void setOnItemClick (AdapterView. onItemClickListener myOnItemClickListener) {58 lvContent. setOnItemClickListener (myOnItemClickListener); 59} 60 61 62 class MyAdapter extends BaseAdapter {63 private List <MenuPopwindowBean> list; 64 private LayoutInflater inflater; 65 66 public MyAdapter (Context context, list <MenuPopwindowBean> list) {67 inflater = LayoutInflater. from (context); 68 this. list = list; 69} 70 71 @ Override 72 public int getCount () {73 return list = null? 0: list. size (); 74} 75 76 @ Override 77 public Object getItem (int position) {78 return list. get (position); 79} 80 81 @ Override 82 public long getItemId (int position) {83 return position; 84} 85 86 @ Override 87 public View getView (int position, view convertView, ViewGroup parent) {88 Holder holder = null; 89 if (convertView = null) {90 convertView = inflater. inflate (R. layout. menu_popup_wind Ow_item, null); 91 holder = new Holder (); 92 holder. ivItem = (ImageView) convertView. findViewById (R. id. iv_menu_item); 93 holder. tvItem = (TextView) convertView. findViewById (R. id. TV _menu_item); 94 convertView. setTag (holder); 95} else {96 holder = (Holder) convertView. getTag (); 97} 98 holder. ivItem. setImageResource (list. get (position ). getIcon (); 99 holder. tvItem. setText (list. get (position ). getText (); 100 return convertView; 101} 102 103 class Holder {104 ImageView ivItem; 105 TextView tvItem; 106} 107} 108 109/** 110 * Show popupWindow111 * 112 * @ param parent113 */114 public void showPopupWindow (View parent) {115 if (! This. isShowing () {116 // The following PULL mode shows popupwindow117 this. showAsDropDown (parent); 118} else {119 this. dismiss (); 120} 121} 122 123 124}
MenuPopwindow
MenuPopwindow layout:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" 6 android:paddingRight="10dp"> 7 8 <ListView 9 android:id="@+id/lv_toptitle_menu"10 android:layout_width="match_parent"11 android:layout_height="wrap_content"12 android:background="@mipmap/back_toptitle_menu" />13 </LinearLayout>
Menu_popup_window
The Item layout file in the Adapter:
1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" 3 android: layout_width = "match_parent" 4 android: layout_height = "match_parent" 5 android: gravity = "center" 6 android: orientation = "horizontal" 7 android: paddingBottom = "10dp" 8 android: paddingTop = "10dp"> 9 10 <ImageView11 android: id = "@ + id/iv_menu_item" 12 android: layout_width = "wrap_content" 13 android: layout_height = "wrap_content" 14 android: layout_gravity = "center_vertical" 15 android: layout_marginRight = "10dp" 16 android: src = "@ mipmap/icon_menu_item_edit"/> 17 18 <TextView19 android: id = "@ + id/TV _menu_item" 20 android: layout_width = "wrap_content" 21 android: layout_height = "wrap_content" 22 android: layout_gravity = "center_vertical" 23 android: text = "test" 24 android: textColor = "@ color/black" 25 android: textSize = "18sp"/> 26 27 </LinearLayout>
Menu_popup_window_item
Usage:
1 int [] icons = {R. mipmap. icon_menu_item_edit, R. mipmap. icon_menu_item_delete}; 2 String [] texts = {"edit", "delete"}; 3 List <MenuPopwindowBean> list = new ArrayList <> (); 4 MenuPopwindowBean = null; 5 for (int I = 0; I <icons. length; I ++) {6 bean = new MenuPopwindowBean (); 7 bean. setIcon (icons [I]); 8 bean. setText (texts [I]); 9 list. add (bean); 10} 11 MenuPopwindow pw = new MenuPopwindow (PersonalImgPlayActivity. this, list); 12 pw. setOnItemClick (myOnItemClickListener); 13 pw. showPopupWindow (findViewById (R. id. img_topdeskshare); // click the button in the upper-right corner.
Click Show events