Android开发教程:Popupwindow的应用


PopupWindow 是一种阻塞式的弹出窗口,这就意味着在我们退出这个弹出框之前,程序会一直等待。它可以浮动在当前Activity的任何的位置上。 需要注意的是,PopupWindow必须触发某个焦点或者某个事件才会显示出来,不然总会会出现错误。

下面是使用PopupWindow 弹出自定义菜单的例子

当我们点击Menu键的时候,会在当前的Activity最下方弹出一个菜单。

效果图如下:

 PopupMenuDemo.java

  1. package com.lingdududu.popupWindow;  
  2.  
  3. import Android.app.Activity;  
  4. import android.content.Context;  
  5. import android.graphics.drawable.BitmapDrawable;  
  6. import android.os.Bundle;  
  7. import android.view.Gravity;  
  8. import android.view.KeyEvent;  
  9. import android.view.LayoutInflater;  
  10. import android.view.Menu;  
  11. import android.view.MenuItem;  
  12. import android.view.View;  
  13. import android.view.ViewGroup;  
  14. import android.widget.BaseAdapter;  
  15. import android.widget.GridView;  
  16. import android.widget.ImageView;  
  17. import android.widget.LinearLayout;  
  18. import android.widget.PopupWindow;  
  19. import android.widget.TextView;  
  20. import android.widget.LinearLayout.LayoutParams;  
  21.  
  22. public class PopupMenuDemo extends Activity {  
  23.       
  24.     private int[] resArray = new int[] {  
  25.             R.drawable.icon_menu_addto, R.drawable.icon_menu_audioinfo,  
  26.             R.drawable.icon_menu_findlrc, R.drawable.icon_menu_scan  
  27.     };  
  28.       
  29.     private String[] title = new String[]{  
  30.             "添加""信息""搜索""查看" 
  31.     };  
  32.       
  33.     private PopupWindow pw = null;  
  34.       
  35.     /** Called when the activity is first created. */ 
  36.     @Override 
  37.     public void onCreate(Bundle savedInstanceState) {  
  38.         super.onCreate(savedInstanceState);  
  39.         setContentView(R.layout.main);  
  40.     }  
  41.       
  42.  
  43.     @Override 
  44.     public boolean onCreateOptionsMenu(Menu menu) {  
  45.         menu.add("");  
  46.         return super.onCreateOptionsMenu(menu);  
  47.           
  48.     }  
  49.       
  50.     /**  
  51.      * 在此方法中实现自定义菜单栏  
  52.      */ 
  53.     public boolean onMenuOpened(int featureId, Menu menu) {  
  54.         if (pw != null) {  
  55.             if (pw.isShowing()) {  
  56.                 // 就关闭弹出窗口  
  57.                 pw.dismiss();  
  58.             }   
  59.             else {  
  60.                 //在指定位置弹出窗口  
  61.                 pw.showAtLocation(findViewById(R.id.tv), Gravity.CENTER, 0300);  
  62.             }  
  63.         }   
  64.         else {  
  65.  
  66.             LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);        
  67.             View view = inflater.inflate(R.layout.menu, null);  
  68.             GridView grid1 = (GridView)view.findViewById(R.id.menuGridChange);  
  69.             grid1.setAdapter(new ImageAdapter(this));  
  70.               
  71.               
  72.             //生成PopupWindow对象  
  73.             pw = new PopupWindow(view,LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);  
  74.               
  75.             //在指定位置弹出窗口  
  76.             pw.showAtLocation(findViewById(R.id.tv), Gravity.CENTER, 0300);  
  77.  
  78.         }  
  79.         // 此处返回false,系统不会执行onCreateOptionsMenu中添加的菜单  
  80.         return false;  
  81.     }  
  82.  
  83.  
  84.     public boolean onKeyDown(int keyCode, KeyEvent event) {  
  85.           
  86.         if(keyCode==KeyEvent.KEYCODE_BACK){  
  87.             pw.dismiss();  
  88.             return false;  
  89.         }  
  90.         return super.onKeyDown(keyCode, event);  
  91.     }  
  92.     @Override 
  93.     public boolean onOptionsItemSelected(MenuItem item) {  
  94.         return super.onOptionsItemSelected(item);  
  95.     }  
  96.  
  97.  
  98.     public class ImageAdapter extends BaseAdapter {  
  99.           
  100.         private Context context;  
  101.           
  102.         public ImageAdapter(Context context) {  
  103.             this.context = context;  
  104.         }  
  105.           
  106.         @Override 
  107.         public int getCount() {  
  108.             return resArray.length;  
  109.         }  
  110.  
  111.         @Override 
  112.         public Object getItem(int arg0) {  
  113.             return resArray[arg0];  
  114.         }  
  115.  
  116.         @Override 
  117.         public long getItemId(int arg0) {  
  118.             return arg0;  
  119.         }  
  120.  
  121.         @Override 
  122.         public View getView(int arg0, View arg1, ViewGroup arg2) {  
  123.             LinearLayout linear = new LinearLayout(context);  
  124.             LinearLayout.LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);  
  125.             linear.setOrientation(LinearLayout.VERTICAL);  
  126.               
  127.             ImageView iv = new ImageView(context);  
  128.             iv.setImageBitmap(((BitmapDrawable)context.getResources().getDrawable(resArray[arg0])).getBitmap());  
  129.             LinearLayout.LayoutParams params2 = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);  
  130.             params2.gravity=Gravity.CENTER;  
  131.             linear.addView(iv, params2);  
  132.               
  133.             TextView tv = new TextView(context);  
  134.             tv.setText(title[arg0]);  
  135.             LinearLayout.LayoutParams params3 = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);  
  136.             params3.gravity=Gravity.CENTER;  
  137.               
  138.             linear.addView(tv, params3);  
  139.               
  140.             return linear;  
  141.         }  
  142.     }  

main.xml

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:orientation="vertical" 
  4.     android:layout_width="fill_parent" 
  5.     android:layout_height="fill_parent" 
  6.     > 
  7. <TextView    
  8.     android:id="@+id/tv" 
  9.     android:layout_width="fill_parent"   
  10.     android:layout_height="wrap_content"   
  11.     android:text="@string/hello" 
  12.     /> 
  13. </LinearLayout> 

menu.xml

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:orientation="vertical" 
  4.     android:layout_width="fill_parent" 
  5.     android:layout_height="fill_parent" 
  6.     > 
  7. <TextView    
  8.     android:id="@+id/tv" 
  9.     android:layout_width="fill_parent"   
  10.     android:layout_height="wrap_content"   
  11.     android:text="@string/hello" 
  12.     /> 
  13. </LinearLayout> 

 

menu_bg_frame.xml 这个文件是菜单背景的样式文件,在drawable-hdpi文件下面

  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <shape android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android"> 
  3.     <solid android:color="#b4000000" /> 
  4.     <stroke android:width="2.0dip" android:color="#b4ffffff" android:dashWidth="3.0dip" android:dashGap="0.0dip" /> 
  5.     <padding android:left="7.0dip" android:top="7.0dip" android:right="7.0dip" android:bottom="7.0dip" /> 
  6.     <corners android:radius="8.0dip" /> 
  7. </shape> 

相关内容