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

Android用PopupWindow实现弹出菜单实例" src="https://img.it610.com/image/product/0792f1d6341940b68ce9dd5faa14e758.jpg" width="215" height="498" style="border:1px solid black;"> (项目总览图) Android用PopupWindow实现弹出菜单实例" src="https://img.it610.com/image/product/6972b26dc52f467b9ce46840746bbf06.jpg" width="184" height="279" style="border:1px solid black;"> (drawable目录截图)

step2:设置应用的UI界面

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

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:id="@+id/main"    ><Button      android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:text="@string/button"    android:onClick="openPopWindow"    /></LinearLayout>


b.弹出菜单的界面,popwindow.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="fill_parent"android:layout_height="fill_parent" android:background="@drawable/rectangle">  <!-- 设置一个手绘的长方形背景 --><GridView android:layout_width="fill_parent"android:layout_height="wrap_content" android:numColumns="4"android:horizontalSpacing="10dp" android:verticalSpacing="10dp"android:id="@+id/gridView" /></LinearLayout>

其中的android:background="@drawable/rectangle"是引用rectangle.xml

<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle"><gradient android:startColor="#1DC9CD" android:endColor="#A2E0FB"android:angle="270" /><padding android:left="2dp" android:top="2dp" android:right="2dp"android:bottom="2dp" /></shape>


c.每个菜单项的界面,grid_item.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"     android:gravity="center"    >    <ImageView          android:layout_width="wrap_content"    android:layout_height="wrap_content"         android:id="@+id/imageView"        /><TextView     android:layout_width="fill_parent"   android:layout_height="wrap_content"   android:gravity="center"   android:textSize="16sp"   android:textColor="#000099"        android:id="@+id/textView"    /></LinearLayout>


d:为菜单设置一个style,用于指定菜单弹出时和退出时的动画效果 styles.xml

<?xml version="1.0" encoding="utf-8"?><resources>    <style name="animation">        <item name="android:windowEnterAnimation">@anim/enter</item> <item name="android:windowExitAnimation">@anim/exit</item>     </style></resources>

其中enter.xml

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"    android:shareInterpolator="false"> <translate        android:fromYDelta="100%p"        android:toYDelta="0"         android:duration="500"        /> <alpha        android:fromAlpha="0.7"        android:toAlpha="1.0"         android:duration="300"        /> </set>

exit.xml

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"    android:shareInterpolator="false"> <translate        android:fromYDelta="0"        android:toYDelta="100%p"         android:duration="2000"        /> <alpha        android:fromAlpha="1.0"        android:toAlpha="0.5"         android:duration="1000"        /> </set>


step3:MainActivity.java

package cn.roco.popwindow;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import android.app.Activity;import android.graphics.drawable.BitmapDrawable;import android.os.Bundle;import android.view.Gravity;import android.view.View;import android.view.ViewGroup;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.GridView;import android.widget.ListAdapter;import android.widget.PopupWindow;import android.widget.SimpleAdapter;public class MainActivity extends Activity {private PopupWindow popupWindow;private View parent;/**菜单弹出来时候的菜单项图案*/private int[] images = { R.drawable.i1, R.drawable.i2, R.drawable.i3,R.drawable.i4, R.drawable.i5, R.drawable.i6, R.drawable.i7,R.drawable.i8 };/**菜单弹出来时候的菜单项文字*/private String[] names = { "搜索", "文件管理", "下载管理", "全屏", "网址", "书签", "加入书签","分享页面" };@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);/**PopupWindow的界面*/View contentView = getLayoutInflater().inflate(R.layout.popwindow, null);/**网格布局界面*/GridView gridView = (GridView) contentView.findViewById(R.id.gridView);/**设置网格布局的适配器*/gridView.setAdapter(getAdapter());/**设置网格布局的菜单项点击时候的Listener*/gridView.setOnItemClickListener(new ItemClickListener());/**初始化PopupWindow*/popupWindow = new PopupWindow(contentView,ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);popupWindow.setFocusable(true);// 取得焦点popupWindow.setBackgroundDrawable(new BitmapDrawable());/**设置PopupWindow弹出和退出时候的动画效果*/popupWindow.setAnimationStyle(R.style.animation);parent = this.findViewById(R.id.main);}private final class ItemClickListener implements OnItemClickListener{@Overridepublic void onItemClick(AdapterView<?> parent, View view, int position,long id) {if (popupWindow.isShowing()) {popupWindow.dismiss();//关闭}}}/**返回网格布局的适配器*/private ListAdapter getAdapter() {List<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();for (int i = 0; i < images.length; i++) {HashMap<String, Object> item = new HashMap<String, Object>();item.put("image", images[i]);item.put("name", names[i]);data.add(item);}SimpleAdapter simpleAdapter = new SimpleAdapter(this, data,R.layout.grid_item, new String[] { "image", "name" },new int[] { R.id.imageView, R.id.textView });return simpleAdapter;}public void openPopWindow(View v) {/**设置PopupWindow弹出后的位置*/popupWindow.showAtLocation(parent, Gravity.BOTTOM, 0, 0);}}

step4:AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"      package="cn.roco.popwindow"      android:versionCode="1"      android:versionName="1.0">    <uses-sdk android:minSdkVersion="8" />    <application android:icon="@drawable/icon" android:label="@string/app_name">        <activity android:name=".MainActivity"                  android:label="@string/app_name">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application></manifest>

step5:运行效果

Android用PopupWindow实现弹出菜单实例" src="https://img.it610.com/image/product/08218ec7b9264b0490ebe92f448f15c9.jpg" width="377" height="535" style="border:1px solid black;">Android用PopupWindow实现弹出菜单实例" src="https://img.it610.com/image/product/ae79c4df868349a293806d0d96964867.jpg" width="374" height="533" style="border:1px solid black;">


==================================================================================================

作者:欧阳鹏 欢迎转载,与人分享是进步的源泉!

转载请保留原文地址:http://blog.csdn.net/ouyang_peng

==================================================================================================


更多相关文章

  1. Windows 安装Calabash-Android
  2. 上中下布局,上下高度指定,中间自适应(左中右同理)
  3. android基本属性
  4. Android设置textview的字体之间的间距
  5. Android(安卓)属性设置失败
  6. android 界面添加返回一栏
  7. Android开发本地及网络Mp3音乐播放器(二十)歌曲下载完成后通知主
  8. Android-2D绘图基础-更新中
  9. Android学习笔记16:Button控件图文混排效果的实现

随机推荐

  1. 如何制作高大上的图表
  2. 从Excel的数据类型说Python
  3. 数据分析,除了Excel透视表,还有什么工具?
  4. 列表是个什么鬼?
  5. 如何培养数据分析的思维?
  6. 新手如何学习SQL
  7. 2019年终总结
  8. Python数据结构:字典那些事儿
  9. 你真的了解参数估计和假设检验吗?
  10. Python数据结构:神奇的序列