step1:新建项目PopWindow,并导入菜单项使用的图片到/res/drawable目录下
(项目总览图) (drawable目录截图)
step2:设置应用的UI界面
a.应用的总体界面,main.xml
01 | <? xml version = "1.0" encoding = "utf-8" ?> |
02 | < LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" |
03 | android:orientation = "vertical" |
04 | android:layout_width = "fill_parent" |
05 | android:layout_height = "fill_parent" |
06 | android:id = "@+id/main" |
09 | android:layout_width = "fill_parent" |
10 | android:layout_height = "wrap_content" |
11 | android:text = "@string/button" |
12 | android:onClick = "openPopWindow" |
b.弹出菜单的界面,popwindow.xml
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" /> |
其中的android:background="@drawable/rectangle"是引用rectangle.xml
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" |
6 | < padding android:left = "2dp" android:top = "2dp" android:right = "2dp" |
7 | android:bottom = "2dp" /> |
c.每个菜单项的界面,grid_item.xml
01 | <? xml version = "1.0" encoding = "utf-8" ?> |
02 | < LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" |
03 | android:layout_width = "match_parent" |
04 | android:layout_height = "match_parent" |
05 | android:orientation = "vertical" |
06 | android:gravity = "center" |
09 | android:layout_width = "wrap_content" |
10 | android:layout_height = "wrap_content" |
11 | android:id = "@+id/imageView" |
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" |
d:为菜单设置一个style,用于指定菜单弹出时和退出时的动画效果 styles.xml
1 | <? xml version = "1.0" encoding = "utf-8" ?> |
3 | < style name = "animation" > |
4 | < item name = "android:windowEnterAnimation" >@anim/enter</ item > |
5 | < item name = "android:windowExitAnimation" >@anim/exit</ item > |
其中enter.xml
01 | <? xml version = "1.0" encoding = "utf-8" ?> |
02 | < set xmlns:android = "http://schemas.android.com/apk/res/android" |
03 | android:shareInterpolator = "false" > |
05 | android:fromYDelta = "100%p" |
07 | android:duration = "500" |
10 | android:fromAlpha = "0.7" |
12 | android:duration = "300" |
exit.xml
01 | <? xml version = "1.0" encoding = "utf-8" ?> |
02 | < set xmlns:android = "http://schemas.android.com/apk/res/android" |
03 | android:shareInterpolator = "false" > |
05 | android:fromYDelta = "0" |
06 | android:toYDelta = "100%p" |
07 | android:duration = "2000" |
10 | android:fromAlpha = "1.0" |
12 | android:duration = "1000" |
step3:MainActivity.java
01 | package cn.roco.popwindow; |
03 | import java.util.ArrayList; |
04 | import java.util.HashMap; |
05 | import java.util.List; |
07 | import android.app.Activity; |
08 | import android.graphics.drawable.BitmapDrawable; |
09 | 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; |
20 | public class MainActivity extends Activity { |
21 | private PopupWindow popupWindow; |
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, |
28 | private String[] names = { "搜索" , "文件管理" , "下载管理" , "全屏" , "网址" , "书签" , "加入书签" , |
32 | public void onCreate(Bundle savedInstanceState) { |
33 | super .onCreate(savedInstanceState); |
34 | setContentView(R.layout.main); |
36 | View contentView = getLayoutInflater() |
37 | .inflate(R.layout.popwindow, null ); |
39 | GridView gridView = (GridView) contentView.findViewById(R.id.gridView); |
41 | gridView.setAdapter(getAdapter()); |
42 | /**设置网格布局的菜单项点击时候的Listener*/ |
43 | gridView.setOnItemClickListener( new ItemClickListener()); |
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); |
53 | parent = this .findViewById(R.id.main); |
56 | private final class ItemClickListener implements OnItemClickListener{ |
58 | public void onItemClick(AdapterView<?> parent, View view, int position, |
60 | if (popupWindow.isShowing()) { |
61 | popupWindow.dismiss(); |
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]); |
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 }); |
81 | public void openPopWindow(View v) { |
82 | /**设置PopupWindow弹出后的位置*/ |
83 | popupWindow.showAtLocation(parent, Gravity.BOTTOM, 0 , 0 ); |
step4:AndroidManifest.xml
01 | <? xml version = "1.0" encoding = "utf-8" ?> |
02 | < manifest xmlns:android = "http://schemas.android.com/apk/res/android" |
03 | package = "cn.roco.popwindow" |
04 | android:versionCode = "1" |
05 | android:versionName = "1.0" > |
06 | < uses-sdk android:minSdkVersion = "8" /> |
08 | < application android:icon = "@drawable/icon" android:label = "@string/app_name" > |
09 | < activity android:name = ".MainActivity" |
10 | android:label = "@string/app_name" > |
12 | < action android:name = "android.intent.action.MAIN" /> |
13 | < category android:name = "android.intent.category.LAUNCHER" /> |
step5:运行效果