Android用PopupWindow实现弹出菜单实例

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

Android用PopupWindow实现弹出菜单实例               (项目总览图)                                        Android用PopupWindow实现弹出菜单实例            (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"
07     >
08 <Button 
09     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

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

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

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"
07     >
08     <ImageView
09          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

1 <?xml version="1.0" encoding="utf-8"?>
2 <resources>
3     <style name="animation">
4         <item name="android:windowEnterAnimation">@anim/enter</item
5      <itemname="android:windowExitAnimation">@anim/exit</item>
6     </style>
7 </resources>

其中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">
04     <translate
05         android:fromYDelta="100%p"
06         android:toYDelta="0"
07         android:duration="500"
08         />
09     <alpha
10         android:fromAlpha="0.7"
11         android:toAlpha="1.0"
12         android:duration="300"
13         />  
14 </set>

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">
04     <translate
05         android:fromYDelta="0"
06         android:toYDelta="100%p"
07         android:duration="2000"
08         />
09     <alpha
10         android:fromAlpha="1.0"
11         android:toAlpha="0.5"
12         android:duration="1000"
13         />  
14 </set>

 

step3:MainActivity.java

01 package cn.roco.popwindow;
02  
03 import java.util.ArrayList;
04 import java.util.HashMap;
05 import java.util.List;
06  
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;
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     privateString[] 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

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" />
07  
08     <application android:icon="@drawable/icon" android:label="@string/app_name">
09         <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用PopupWindow实现弹出菜单实例                         Android用PopupWindow实现弹出菜单实例

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值