<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" match_parent " android:layout_height=" Match_parent " android: Background= "#DA4F30" android:cachecolorhint= "#000000" android:orientation= "vertical" > < ListView android:id= "@+id/lv_dialog" android:layout_width= "match_parent" android:layout_height= " Match_parent " android:cachecolorhint=" #00000000 " android:scrollbars=" None " android:o Verscrollmode= "Never" > </ListView></LinearLayout>
Package Com.example.showpop;import Android.app.activity;import Android.content.context;import Android.graphics.drawable.bitmapdrawable;import Android.os.bundle;import Android.view.gravity;import Android.view.layoutinflater;import Android.view.view;import Android.view.view.onclicklistener;import Android.view.window;import Android.view.windowmanager;import Android.widget.adapterview;import Android.widget.adapterview.onitemclicklistener;import Android.widget.arrayadapter;import Android.widget.imageview;import Android.widget.linearlayout;import Android.widget.listview;import Android.widget.popupwindow;public class Mainactivity extends Activity {private LinearLayout layout;private ListView Listview;private Popupwindow popupwindow;private String title[] = {"1", "2", "3", "4", "5"};p rivate ImageView ImageView ; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Requestwindowfeature (Window.feature_no_title); Setcontentview (r.layout.activity_main); IMageview= (ImageView) Findviewbyid (R.id.imageview_above_more); Imageview.setonclicklistener (new Onclicklistener () {@ overridepublic void OnClick (View v) {Showpopupwindow (ImageView);}}); public void Showpopupwindow (View parent) {//Load layout layouts = (linearlayout) layoutinflater.from (mainactivity.this). Inflate (r.layout.dialog, NULL);//Find layout control in ListView = (ListView) Layout.findviewbyid (R.id.lv_dialog);// Set Adapter Listview.setadapter (New arrayadapter<string> (Mainactivity.this,r.layout.text, R.id.tv_text, title));// Instantiate Popupwindowpopupwindow = new Popupwindow (layout, 300,500);//Controls whether the keyboard can get focus popupwindow.setfocusable (TRUE);// Sets the background popupwindow.setbackgrounddrawable of the Popupwindow popup form (new bitmapdrawable (null, "")); WindowManager manager= (WindowManager) Getsystemservice (Context.window_service); @SuppressWarnings ("deprecation")/ /Get Xoffint Xpos=manager.getdefaultdisplay (). GetWidth ()/2-popupwindow.getwidth ()/2;//xoff,yoff offsets based on the lower left corner of the anchor. Popupwindow.showasdropdown (parent,xpos, 0);//monitor Listview.setonitemclicklistener (nEW Onitemclicklistener () {@Overridepublic void Onitemclick (adapterview<?> arg0, View arg1, int arg2,long arg3) {//Off Closed Popupwindowpopupwindow.dismiss ();p Opupwindow = null;}});}}Source Code