step1:新建项目PopWindow,并导入菜单项使用的图片到/res/drawable目录下
(项目总览图) (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 = { "搜索", "文件管理", "下载管理", "全屏", "网址", "书签", "加入书签",
"分享页面" };
@Override
public 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{
@Override
public 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:运行效果
==================================================================================================
作者:欧阳鹏 欢迎转载,与人分享是进步的源泉!
转载请保留原文地址:http://blog.csdn.net/ouyang_peng
==================================================================================================
分享到:
相关推荐
Android高手进阶教程之----Android PopupWindow的使用!!! .doc Android高手进阶教程之----Android 数据库SQLiteDatabase的使用!.doc Android高手进阶教程之----Android 通用获取Ip的方法(判断手机是否联网的方法)...
安卓Android源码——Android之用PopupWindow实现弹出菜单.zip
之用PopupWindow实现弹出菜单.zip
在使用UC-WebBrowser时,你会发现它的弹出菜单跟系统自带的菜单不一样。它实现更多菜单选项的显示和分栏。其实,它的本身是PopupWindow或者是AlertDialog对话框,在里面添加两个GridView控件,一个是菜单标题栏,一...
Android PopupWindow实现右侧、左侧和底部弹出菜单 效果图:http://blog.csdn.net/jianfengwen/article/details/48629217
Android之用PopupWindow实现弹出listview形式菜单.rar
Android之用PopupWindow实现弹出菜单.rar
Android PopupWindow实现弹出菜单.zip
Android从屏幕底部滑动弹出PopupWindow,有动画效果,类似于sharesdk的分享页面滑动弹出效果。
大家可自行学习,研究,并补充该demo.
Android PopupWindow弹出实例 xml方式配置动画效果
myandroidmenu_PopupWindow实现Android应用的弹出菜单.rar
Android 单击listview弹出popupwindow弹出框,里面还附带listview右滑删除按钮。博客网址:http://blog.csdn.net/qq_16064871
Android--开发--PopupWindow下拉列表
在android 开发中,让popupwindow 弹出在点击按钮的上方中间; 说明文档见我的博客
androud实现PopupWindow弹出菜单简单实例
安卓开发-Android之用PopupWindow实现弹出菜单.zip
android页面开发中会碰到这种需求:点击页面顶部工具栏某个按钮时需要弹出下拉菜单。该工程利用PopupWindow实现了点击工具栏弹出下拉菜单的功能的主要实现类 .
Android源码——PopupWindow实现弹出菜单.zip
Android安卓源码_PopupWindow弹出菜单