See http://blog.csdn.net/u013210620/article/details/46011945
Mainactivity
Package Com.example.mypopupwindow;import Java.util.arraylist;import Java.util.hashmap;import java.util.List;import Java.util.map;import Android.app.activity;import Android.content.context;import Android.graphics.Rect;import Android.graphics.drawable.bitmapdrawable;import Android.os.bundle;import Android.view.gravity;import Android.view.view;import Android.view.view.onclicklistener;import Android.view.window;import Android.view.windowmanager;import Android.view.animation.animation;import Android.view.animation.animation.animationlistener;import Android.view.animation.scaleanimation;import Android.widget.adapterview;import Android.widget.adapterview.onitemclicklistener;import Android.widget.ImageView ; Import Android.widget.linearlayout.layoutparams;import Android.widget.listview;import Android.widget.PopupWindow ; Import Android.widget.simpleadapter;import Android.widget.toast;public class Mainactivity extends Activity Implements Onclicklistener {private Popupwindow msmsmorepopup;private ViEW smsmorepopulayout; List<map<string, string>> smsmorelist;private ImageView iv_common_more; @Overrideprotected void OnCreate ( Bundle savedinstancestate) {super.oncreate (savedinstancestate); Requestwindowfeature (Window.feature_no_title); Setcontentview (r.layout.activity_main); Iv_common_more = (ImageView) Findviewbyid (r.id.iv_common_more); iv_common_ More.setonclicklistener (this); Initsmsmorepopudata (); Initpopup (); Msmsmorepopup.setondismisslistener (new Popupwindow.ondismisslistener () {@Overridepublic void Ondismiss () {Popupexitalpha ();}}); private void Initsmsmorepopudata () {smsmorelist = new arraylist<map<string, string>> (); map<string, string> map;map = new hashmap<string, string> (); Map.put ("Sms_more", "emptying Information"); Smsmorelist.add ( map); map = new hashmap<string, string> (); Map.put ("Sms_more", "Backup"); Smsmorelist.add (map); map = new hashmap< String, string> (); Map.put ("Sms_more", "import"); Smsmorelist.add (map); map = new hashmap<string, string> (); Map.put ("sMs_more "," recharge payment "); Smsmorelist.add (map); map = new hashmap<string, string> (); Map.put (" Sms_more "," settings "); Smsmorelist.add (map), map = new hashmap<string, string> (), Map.put ("Sms_more", "Exit Login"); Smsmorelist.add (map);} private void Initpopup () {smsmorepopulayout = view.inflate (this,r.layout.popupwindow_layout_right, NULL); ListView mmorepopuplist = (ListView) Smsmorepopulayout.findviewbyid (r.id.lv_popup_list); msmsmorepopup = new Popupwindow (smsmorepopulayout); Msmsmorepopup.setwidth (layoutparams.wrap_content); MSmsMorePopup.setHeight ( layoutparams.wrap_content); Mmorepopuplist.setadapter (New Simpleadapter (Mainactivity.this,smsmorelist, R.layout.list_item_popupwindow,new string[] {"Sms_more"}, new int[] {r.id.tv_list_item}); Mmorepopuplist.setonitemclicklistener (New Onitemclicklistener () {@Overridepublic void Onitemclick (adapterview<? > Parent, View view,int position, long ID) {switch (position) {case 0://Mdialcontroller.dialmenu (dialcontroller.clear ); Toast.maketext (mainactivity.tHis, "Toast Test popup emptied record", 0). Show (); Morepopuexit (); Break;case 1:toast.maketext (Mainactivity.this, "Toast Test popup Backup", 0). Show (); Morepopuexit (); Break;case 2:toast.maketext (Mainactivity.this, "Toast Test popup Import", 0). Show (); Morepopuexit (); Break;case 3:toast.maketext (mainactivity.this, "Toast Test popup Recharge Payment", 0). Show (); Morepopuexit (); Break;case 4: Toast.maketext (Mainactivity.this, "Toast Test Popup Settings", 0). Show (); Morepopuexit (); break;case 5:morepopuexit (); break; Default:break;}} private void Morepopuexit () {scaleanimation animation = new Scaleanimation (1f, 0f, 1f, 0f,animation.relative_to_self, 1.0 F,animation.relative_to_self, 0.0f); animation.setduration (+); Animation.setfillafter (true); Smsmorepopulayout.startanimation (animation);p Opupexitalpha (); Animation.setanimationlistener (new Animationlistener () {@Overridepublic void Onanimationstart (Animation Animation) {} @Overridepublic void Onanimationrepeat (Animation Animation) {} @Overridepublic void Onanimationend (Animation Animation) { Msmsmorepopup.dismiss ();//Close}});}); MsmsmOrepopup.setfocusable (True); Msmsmorepopup.setbackgrounddrawable (new bitmapdrawable ()); Msmsmorepopup.setanimationstyle (r.style.animation); msmsmorepopup.setoutsidetouchable (true);} private void Popupshowalpha () {Windowmanager.layoutparams params = GetWindow (). GetAttributes ();p arams.alpha = 0.6f; GetWindow (). SetAttributes (params);} private void Popupexitalpha () {Windowmanager.layoutparams params = GetWindow (). GetAttributes ();p arams.alpha = 1.0f; GetWindow (). SetAttributes (params);} public int dp2px (context context, float DP) {final FLOAT scale = context.getresources (). Getdisplaymetrics (). density; return (int) (DP * scale + 0.5f);} private void onmesurelocation (view view, Popupwindow popup) {Rect frame = new Rect (); GetWindow (). Getdecorview (). Getwindowvisibledisplayframe (frame), int mmorepopupmargintop = Frame.top + dp2px (mainactivity.this, n); int Mmorepopupmarginright = dp2px (mainactivity.this, +);p Opupshowalpha ();p opup.showatlocation (FindViewById (R.id.rl_ Root), gravity.right| Gravity.top, MmorepOpupmarginright, Mmorepopupmargintop); scaleanimation animation = new Scaleanimation (0f, 1f, 0f, 1f,animation.relative_to_self, 1.0f, animation.relative_to_ self,0.0f); animation.setduration (+); Animation.setfillafter (true); view.startanimation (animation);} @Overridepublic void OnClick (View v) {switch (V.getid ()) {Case r.id.iv_common_more:if (msmsmorepopup.isshowing ()) { Msmsmorepopup.dismiss ();//Close}onmesurelocation (Smsmorepopulayout, msmsmorepopup); break;default:break;}}}
Activity_main.xml
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Android : id= "@+id/rl_root" tools:context= "com.example.popupwindowvslistview.MainActivity" > <relativelayout Android:id= "@+id/rl_common_default" android:layout_width= "fill_parent" android:layout_height= "48DP" android:background= "@drawable/top_bg" > <imageview android:id= "@+id/iv_common_more "Android:layout_width=" 34DP "android:layout_height=" 34DP "android:padding=" 5DP " Android:layout_alignparentright= "true" android:layout_centervertical= "true" android:l ayout_marginright= "15DP" android:src= "@drawable/top_more_n" android:visibility= "visible"/> </RelativeLayout> </relativElayout>
List_item_popupwindow.xml
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" fill_parent " android:layout_height=" fill_parent " android:gravity=" Left " android:orientation=" vertical "> <textview android:id=" @+id/tv_list_item " android: Layout_width= "Fill_parent" android:layout_height= "35DP" android:textsize= "16DP" android:textcolor= "#111111" android:text= "AAA" android:gravity= "center" android:background= "@drawable/popupwindow _list_item_text_selector " /></linearlayout>
Popupwindow_layout_right.xml
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" fill_parent " android:layout_height=" fill_parent "> <listview android:id= "@+id/lv_popup_list" android:layout_width= "110DP" android:layout_height= "Wrap_content" android:background= "@drawable/layer_popup" android:focusableintouchmode= "true" android:divider= "@ Null " android:listselector=" @drawable/popupwindow_list_item_text_selector " android:paddingbottom=" 5DP " android:paddingtop=" 5DP " android:scrollbars=" None "/></linearlayout>
Layer_popup.xml
<?xml version= "1.0" encoding= "Utf-8"?> <layer-list xmlns:android= "Http://schemas.android.com/apk/res /android "> <!--Bottom 2DP Shadow- - <item> <shape android:shape=" Rectangle " > <solid android:color= "#d9d9d9"/> <corners android:radius= "5DP"/> </shape> </item> <!--white Top color-- <item android:bottom= "5px" > <shape Android:shape= "Rectangle" > <solid android:color= "#d9d9d9"/> <corners android:radius= "5DP"/ > </shape> </item> </layer-list> <!-- --
Popupwindow_list_item_text_selector.xml
<?xml version= "1.0" encoding= "Utf-8"? ><selector xmlns:android= "http://schemas.android.com/apk/res/ Android "> <item android:state_pressed=" true " android:drawable=" @drawable/more_popu_normal "/> <item android:drawable= "@drawable/more_popu_pressed"/></selector>
Out.xml
<?xml version= "1.0" encoding= "Utf-8"? ><set xmlns:android= "Http://schemas.android.com/apk/res/android" > <!-- displacement effect fromxdelta= "0%p" refers to the x-axis that is relative to the parent control from the parent control's x=0 out of the start displacement fromydelta= "0%p" refers to the y-axis relative to the parent control from the parent control's y= 0 out start displacement toxdelta= "100%p" refers to the x-axis of the parent control that reaches the x=100 position of the parent control, which is the x-axis at the end of the screen toydelta= "100%p" refers to the y-axis relative to the parent control reaching the parent control y= The 100 position is the x-axis at the end of the screen-- <scale android:duration= "+" android:pivotx= "100%" android: Pivoty= "0%" android:fromxscale= "1.0" android:toxscale= "0.0" android:fromyscale= "1.0" android:toyscale= "0.0"/> </set>
Color.xml
<?xml version= "1.0" encoding= "Utf-8"?> <resources> <drawable name= "more_popu_pressed" ># d9d9d9</drawable> <drawable name= "More_popu_normal" > #ffffff </drawable> </resources >
<style name= "Animation" > <item name= "android:windowexitanimation" > @anim/out</item> </style>
Custom Popupwindow+simpleadapter+anim