Popupwindow的使用

来源:互联网 发布:淘宝内衣买家秀 编辑:程序博客网 时间:2024/05/19 01:13

项目中经常会使用到popupwindow做菜单选项,这个类在使用中有一些经常被遗忘的细节,今天用一个实例来总结一个popupwindow较常用的用法。

 

效果图:

 

 

 

MainActivity.java:

Java代码  收藏代码
  1. public class MainActivity extends Activity {  
  2.   
  3.     private ImageButton ibOperationMore;  
  4.   
  5.     List<Map<String, String>> moreList;  
  6.     private PopupWindow pwMyPopWindow;// popupwindow  
  7.     private ListView lvPopupList;// popupwindow中的ListView  
  8.     private int NUM_OF_VISIBLE_LIST_ROWS = 3;// 指定popupwindow中Item的数量  
  9.   
  10.     @Override  
  11.     protected void onCreate(Bundle savedInstanceState) {  
  12.         super.onCreate(savedInstanceState);  
  13.         setContentView(R.layout.activity_main);  
  14.   
  15.         iniData();  
  16.   
  17.         iniPopupWindow();  
  18.   
  19.         // 更多操作按钮  
  20.         ibOperationMore = (ImageButton) findViewById(R.id.ib_operate_more);  
  21.         ibOperationMore.setOnClickListener(new OnClickListener() {  
  22.   
  23.             @Override  
  24.             public void onClick(View v) {  
  25.   
  26.                 if (pwMyPopWindow.isShowing()) {  
  27.   
  28.                     pwMyPopWindow.dismiss();// 关闭  
  29.                 } else {  
  30.   
  31.                     pwMyPopWindow.showAsDropDown(ibOperationMore);// 显示  
  32.                 }  
  33.   
  34.             }  
  35.         });  
  36.     }  
  37.   
  38.     private void iniData() {  
  39.   
  40.         moreList = new ArrayList<Map<String, String>>();  
  41.         Map<String, String> map;  
  42.         map = new HashMap<String, String>();  
  43.         map.put("share_key""复制");  
  44.         moreList.add(map);  
  45.         map = new HashMap<String, String>();  
  46.         map.put("share_key""删除");  
  47.         moreList.add(map);  
  48.         map = new HashMap<String, String>();  
  49.         map.put("share_key""修改");  
  50.         moreList.add(map);  
  51.     }  
  52.   
  53.     private void iniPopupWindow() {  
  54.   
  55.         LayoutInflater inflater = (LayoutInflater) this  
  56.                 .getSystemService(LAYOUT_INFLATER_SERVICE);  
  57.         View layout = inflater.inflate(R.layout.task_detail_popupwindow, null);  
  58.         lvPopupList = (ListView) layout.findViewById(R.id.lv_popup_list);  
  59.         pwMyPopWindow = new PopupWindow(layout);  
  60.         pwMyPopWindow.setFocusable(true);// 加上这个popupwindow中的ListView才可以接收点击事件  
  61.   
  62.         lvPopupList.setAdapter(new SimpleAdapter(MainActivity.this, moreList,  
  63.                 R.layout.list_item_popupwindow, new String[] { "share_key" },  
  64.                 new int[] { R.id.tv_list_item }));  
  65.         lvPopupList.setOnItemClickListener(new OnItemClickListener() {  
  66.   
  67.             @Override  
  68.             public void onItemClick(AdapterView<?> parent, View view,  
  69.                     int position, long id) {  
  70.   
  71.                 Toast.makeText(MainActivity.this,  
  72.                         moreList.get(position).get("share_key"),  
  73.                         Toast.LENGTH_LONG).show();  
  74.             }  
  75.         });  
  76.   
  77.         // 控制popupwindow的宽度和高度自适应  
  78.         lvPopupList.measure(View.MeasureSpec.UNSPECIFIED,  
  79.                 View.MeasureSpec.UNSPECIFIED);  
  80.         pwMyPopWindow.setWidth(lvPopupList.getMeasuredWidth());  
  81.         pwMyPopWindow.setHeight((lvPopupList.getMeasuredHeight() + 20)  
  82.                 * NUM_OF_VISIBLE_LIST_ROWS);  
  83.   
  84.         // 控制popupwindow点击屏幕其他地方消失  
  85.         pwMyPopWindow.setBackgroundDrawable(this.getResources().getDrawable(  
  86.                 R.drawable.bg_popupwindow));// 设置背景图片,不能在布局中设置,要通过代码来设置  
  87.         pwMyPopWindow.setOutsideTouchable(true);// 触摸popupwindow外部,popupwindow消失。这个要求你的popupwindow要有背景图片才可以成功,如上  
  88.     }  
  89.   
  90. }  

 

activity_main.xml:

Java代码  收藏代码
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     tools:context=".MainActivity"  
  6.     android:background="#ffffff" >  
  7.   
  8.     <LinearLayout  
  9.         android:id="@+id/ll_head_bar"  
  10.         style="@style/header_linear_layout"  
  11.         android:layout_alignParentTop="true" >  
  12.   
  13.         <Button  
  14.             android:id="@+id/btn_back"  
  15.             style="@style/header_button_back"  
  16.             android:layout_marginLeft="8dip" />  
  17.   
  18.         <TextView  
  19.             style="@style/header_text_view"  
  20.             android:visibility="invisible" />  
  21.   
  22.         <ImageButton  
  23.             android:id="@+id/ib_operate_more"  
  24.             style="@style/header_button_operate"  
  25.             android:layout_marginLeft="8dip"  
  26.             android:layout_marginRight="10dip"  
  27.             android:src="@drawable/ico_headbar_more" />  
  28.     </LinearLayout>  
  29.   
  30. </RelativeLayout>  

 

list_item_popupwindow.xml:

Java代码  收藏代码
  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="vertical" >  
  7.       
  8.     <TextView   
  9.         android:id="@+id/tv_list_item"  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content"  
  12.         android:gravity="center"  
  13.         android:minHeight="40dp"  
  14.         android:minWidth="120dp"  
  15.         android:textSize="20sp"  
  16.         android:textColor="@color/popupwindow_list_item_text_selector"  
  17.         />  
  18.   
  19. </LinearLayout>  

 

task_detail_popupwindow.xml:

Java代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="wrap_content"  
  4.     android:layout_height="wrap_content"  
  5.     android:orientation="vertical"  
  6.     >"  
  7.   
  8.     <ListView  
  9.         android:id="@+id/lv_popup_list"  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content"  
  12.         android:listSelector="#00000000"  
  13.         android:divider="@drawable/ico_list_item_line_popupwindow"  
  14.         android:focusableInTouchMode="true"  
  15.          />  
  16.   
  17. </LinearLayout>  

 

 

注意:如果你想让自己的popupwindow在Activity启动的时候就显示的话,不能放在onCreate方法中,因为这个时候,界面组件还未加载好,所以会提示你你的Activity是否已经启动了,你可以放在onAttachedToWindow方法中。

 

关于显示的位置还有一个方法showAtLocation(parent, gravity, x, y)

这里的参数parent指的是这个popupwindow相对的是哪个父类,比如你想要让这个popupwindow相对于整个屏幕那么你的parent可以是

Java代码  收藏代码
  1. View parent = this.getWindow().getDecorView();  

 parent.getHeight()得到的值是你的屏幕的高度(像素)。

这个时候,x,y就是它在这个parent中的相对位置了。Gravity默认是左上Left,Top

 例如:下面的代码设置了popupwindow在屏幕的左下角的位置。

Java代码  收藏代码
  1. View parent = this.getWindow().getDecorView();//高度为手机实际的像素高度  
  2. pwMyPopWindow.showAtLocation(parent, Gravity.NO_GRAVITY, (int)(parent.getWidth()/18), parent.getHeight() - btnAddTask.getHeight() - (int)(parent.getHeight()/9));  

  

 

还有一些style,color之类的属性这里就不贴出来了。有兴趣的同学可以下载实例下来看看

 

项目地址:

https://github.com/michaelye/PopupwindowDemo.git

 

 

 

0 0
原创粉丝点击