我的Android进阶之旅------>Android用PopupWindow实现弹出菜单实例

http://blog.csdn.net/ouyang_peng/article/details/8801741

step1:新建项目PopWindow,并导入菜单项使用的图片到/res/drawable目录下

我的Android进阶之旅------>Android用PopupWindow实现弹出菜单实例_第1张图片              (项目总览图)                                        我的Android进阶之旅------>Android用PopupWindow实现弹出菜单实例_第2张图片           (drawable目录截图)       

step2:设置应用的UI界面 

a.应用的总体界面,main.xml

[html] view plain copy
  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.     android:id="@+id/main"  
  7.     >  
  8. <Button    
  9.     android:layout_width="fill_parent"   
  10.     android:layout_height="wrap_content"   
  11.     android:text="@string/button"  
  12.     android:onClick="openPopWindow"  
  13.     />  
  14. </LinearLayout>  


b.弹出菜单的界面,popwindow.xml
[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical" android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent" android:background="@drawable/rectangle">  <!-- 设置一个手绘的长方形背景 -->  
  5.     <GridView android:layout_width="fill_parent"  
  6.         android:layout_height="wrap_content" android:numColumns="4"  
  7.         android:horizontalSpacing="10dp" android:verticalSpacing="10dp"  
  8.         android:id="@+id/gridView" />  
  9. </LinearLayout>  

其中的android:background="@drawable/rectangle"是引用rectangle.xml
[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:shape="rectangle">  
  4.     <gradient android:startColor="#1DC9CD" android:endColor="#A2E0FB"  
  5.         android:angle="270" />  
  6.     <padding android:left="2dp" android:top="2dp" android:right="2dp"  
  7.         android:bottom="2dp" />  
  8. </shape>  


c.每个菜单项的界面,grid_item.xml
[html] view plain copy
  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:gravity="center"  
  7.     >  
  8.     <ImageView   
  9.          android:layout_width="wrap_content"  
  10.          android:layout_height="wrap_content"  
  11.          android:id="@+id/imageView"  
  12.         />  
  13.     <TextView   
  14.         android:layout_width="fill_parent"  
  15.         android:layout_height="wrap_content"  
  16.         android:gravity="center"  
  17.         android:textSize="16sp"  
  18.         android:textColor="#000099"  
  19.         android:id="@+id/textView"  
  20.         />  
  21. </LinearLayout>  


d:为菜单设置一个style,用于指定菜单弹出时和退出时的动画效果  styles.xml
[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <style name="animation">  
  4.         <item name="android:windowEnterAnimation">@anim/enter</item>   
  5.      <item name="android:windowExitAnimation">@anim/exit</item>   
  6.     </style>  
  7. </resources>  

其中enter.xml
[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:shareInterpolator="false">  
  4.     <translate  
  5.         android:fromYDelta="100%p"  
  6.         android:toYDelta="0"   
  7.         android:duration="500"  
  8.         />  
  9.     <alpha  
  10.         android:fromAlpha="0.7"  
  11.         android:toAlpha="1.0"   
  12.         android:duration="300"  
  13.         />     
  14. </set>  

exit.xml
[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:shareInterpolator="false">  
  4.     <translate  
  5.         android:fromYDelta="0"  
  6.         android:toYDelta="100%p"   
  7.         android:duration="2000"  
  8.         />  
  9.     <alpha  
  10.         android:fromAlpha="1.0"  
  11.         android:toAlpha="0.5"   
  12.         android:duration="1000"  
  13.         />     
  14. </set>  


step3:MainActivity.java

[java] view plain copy
  1. package cn.roco.popwindow;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.HashMap;  
  5. import java.util.List;  
  6.   
  7. import android.app.Activity;  
  8. import android.graphics.drawable.BitmapDrawable;  
  9. import android.os.Bundle;  
  10. import android.view.Gravity;  
  11. import android.view.View;  
  12. import android.view.ViewGroup;  
  13. import android.widget.AdapterView;  
  14. import android.widget.AdapterView.OnItemClickListener;  
  15. import android.widget.GridView;  
  16. import android.widget.ListAdapter;  
  17. import android.widget.PopupWindow;  
  18. import android.widget.SimpleAdapter;  
  19.   
  20. public class MainActivity extends Activity {  
  21.     private PopupWindow popupWindow;  
  22.     private View parent;  
  23.     /**菜单弹出来时候的菜单项图案*/  
  24.     private int[] images = { R.drawable.i1, R.drawable.i2, R.drawable.i3,  
  25.             R.drawable.i4, R.drawable.i5, R.drawable.i6, R.drawable.i7,  
  26.             R.drawable.i8 };  
  27.     /**菜单弹出来时候的菜单项文字*/  
  28.     private String[] names = { "搜索""文件管理""下载管理""全屏""网址""书签""加入书签",  
  29.             "分享页面" };  
  30.   
  31.     @Override  
  32.     public void onCreate(Bundle savedInstanceState) {  
  33.         super.onCreate(savedInstanceState);  
  34.         setContentView(R.layout.main);  
  35.         /**PopupWindow的界面*/  
  36.         View contentView = getLayoutInflater()  
  37.                 .inflate(R.layout.popwindow, null);  
  38.         /**网格布局界面*/  
  39.         GridView gridView = (GridView) contentView.findViewById(R.id.gridView);  
  40.         /**设置网格布局的适配器*/  
  41.         gridView.setAdapter(getAdapter());  
  42.         /**设置网格布局的菜单项点击时候的Listener*/  
  43.         gridView.setOnItemClickListener(new ItemClickListener());  
  44.         /**初始化PopupWindow*/  
  45.         popupWindow = new PopupWindow(contentView,  
  46.                 ViewGroup.LayoutParams.MATCH_PARENT,  
  47.                 ViewGroup.LayoutParams.WRAP_CONTENT);  
  48.         popupWindow.setFocusable(true);// 取得焦点  
  49.         popupWindow.setBackgroundDrawable(new BitmapDrawable());  
  50.         /**设置PopupWindow弹出和退出时候的动画效果*/  
  51.         popupWindow.setAnimationStyle(R.style.animation);  
  52.           
  53.         parent = this.findViewById(R.id.main);  
  54.     }  
  55.       
  56.     private final class ItemClickListener implements OnItemClickListener{  
  57.         @Override  
  58.         public void onItemClick(AdapterView<?> parent, View view, int position,  
  59.                 long id) {  
  60.             if (popupWindow.isShowing()) {  
  61.                 popupWindow.dismiss();//关闭  
  62.             }  
  63.         }  
  64.     }  
  65.       
  66.     /**返回网格布局的适配器*/  
  67.     private ListAdapter getAdapter() {  
  68.         List<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();  
  69.         for (int i = 0; i < images.length; i++) {  
  70.             HashMap<String, Object> item = new HashMap<String, Object>();  
  71.             item.put("image", images[i]);  
  72.             item.put("name", names[i]);  
  73.             data.add(item);  
  74.         }  
  75.         SimpleAdapter simpleAdapter = new SimpleAdapter(this, data,  
  76.                 R.layout.grid_item, new String[] { "image""name" },  
  77.                 new int[] { R.id.imageView, R.id.textView });  
  78.         return simpleAdapter;  
  79.     }  
  80.   
  81.     public void openPopWindow(View v) {  
  82.         /**设置PopupWindow弹出后的位置*/  
  83.         popupWindow.showAtLocation(parent, Gravity.BOTTOM, 00);  
  84.     }  
  85. }  

step4:AndroidManifest.xml
[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.       package="cn.roco.popwindow"  
  4.       android:versionCode="1"  
  5.       android:versionName="1.0">  
  6.     <uses-sdk android:minSdkVersion="8" />  
  7.   
  8.     <application android:icon="@drawable/icon" android:label="@string/app_name">  
  9.         <activity android:name=".MainActivity"  
  10.                   android:label="@string/app_name">  
  11.             <intent-filter>  
  12.                 <action android:name="android.intent.action.MAIN" />  
  13.                 <category android:name="android.intent.category.LAUNCHER" />  
  14.             </intent-filter>  
  15.         </activity>  
  16.   
  17.     </application>  
  18. </manifest>  

step5:运行效果

我的Android进阶之旅------>Android用PopupWindow实现弹出菜单实例_第3张图片                        我的Android进阶之旅------>Android用PopupWindow实现弹出菜单实例_第4张图片


  ==================================下面看一个gif动画===========================================     

                                                                  我的Android进阶之旅------>Android用PopupWindow实现弹出菜单实例_第5张图片



你可能感兴趣的:(我的Android进阶之旅------>Android用PopupWindow实现弹出菜单实例)