PopupWindow 是一种阻塞式的弹出窗口,这就意味着在我们退出这个弹出框之前,程序会一直等待。它可以浮动在当前Activity的任何的位置上。 需要注意的是,PopupWindow必须触发某个焦点或者某个事件才会显示出来,不然总会会出现错误。
下面是使用PopupWindow 弹出自定义菜单的例子
当我们点击Menu键的时候,会在当前的Activity最下方弹出一个菜单。
效果图如下:
PopupMenuDemo.java
- package com.lingdududu.popupWindow;
-
- import android.app.Activity;
- import android.content.Context;
- import android.graphics.drawable.BitmapDrawable;
- import android.os.Bundle;
- import android.view.Gravity;
- import android.view.KeyEvent;
- import android.view.LayoutInflater;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.BaseAdapter;
- import android.widget.GridView;
- import android.widget.ImageView;
- import android.widget.LinearLayout;
- import android.widget.PopupWindow;
- import android.widget.TextView;
- import android.widget.LinearLayout.LayoutParams;
-
- public class PopupMenuDemo extends Activity {
-
- private int[] resArray = new int[] {
- R.drawable.icon_menu_addto, R.drawable.icon_menu_audioinfo,
- R.drawable.icon_menu_findlrc, R.drawable.icon_menu_scan
- };
-
- private String[] title = new String[]{
- "添加", "信息", "搜索", "查看"
- };
-
- private PopupWindow pw = null;
-
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- }
-
-
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- menu.add("");
- return super.onCreateOptionsMenu(menu);
-
- }
-
-
-
-
- public boolean onMenuOpened(int featureId, Menu menu) {
- if (pw != null) {
- if (pw.isShowing()) {
-
- pw.dismiss();
- }
- else {
-
- pw.showAtLocation(findViewById(R.id.tv), Gravity.CENTER, 0, 300);
- }
- }
- else {
-
- LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- View view = inflater.inflate(R.layout.menu, null);
- GridView grid1 = (GridView)view.findViewById(R.id.menuGridChange);
- grid1.setAdapter(new ImageAdapter(this));
-
-
-
- pw = new PopupWindow(view,LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
-
-
- pw.showAtLocation(findViewById(R.id.tv), Gravity.CENTER, 0, 300);
-
- }
-
- return false;
- }
-
-
- public boolean onKeyDown(int keyCode, KeyEvent event) {
-
- if(keyCode==KeyEvent.KEYCODE_BACK){
- pw.dismiss();
- return false;
- }
- return super.onKeyDown(keyCode, event);
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- return super.onOptionsItemSelected(item);
- }
-
-
- public class ImageAdapter extends BaseAdapter {
-
- private Context context;
-
- public ImageAdapter(Context context) {
- this.context = context;
- }
-
- @Override
- public int getCount() {
- return resArray.length;
- }
-
- @Override
- public Object getItem(int arg0) {
- return resArray[arg0];
- }
-
- @Override
- public long getItemId(int arg0) {
- return arg0;
- }
-
- @Override
- public View getView(int arg0, View arg1, ViewGroup arg2) {
- LinearLayout linear = new LinearLayout(context);
- LinearLayout.LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
- linear.setOrientation(LinearLayout.VERTICAL);
-
- ImageView iv = new ImageView(context);
- iv.setImageBitmap(((BitmapDrawable)context.getResources().getDrawable(resArray[arg0])).getBitmap());
- LinearLayout.LayoutParams params2 = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
- params2.gravity=Gravity.CENTER;
- linear.addView(iv, params2);
-
- TextView tv = new TextView(context);
- tv.setText(title[arg0]);
- LinearLayout.LayoutParams params3 = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
- params3.gravity=Gravity.CENTER;
-
- linear.addView(tv, params3);
-
- return linear;
- }
- }
- }
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"
- >
- <TextView
- android:id="@+id/tv"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/hello"
- />
- </LinearLayout>
menu.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"
- >
- <TextView
- android:id="@+id/tv"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/hello"
- />
- </LinearLayout>
menu_bg_frame.xml 这个文件是菜单背景的样式文件,在drawable-hdpi文件下面
- <?xml version="1.0" encoding="UTF-8"?>
- <shape android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android">
- <solid android:color="#b4000000" />
- <stroke android:width="2.0dip" android:color="#b4ffffff" android:dashWidth="3.0dip" android:dashGap="0.0dip" />
- <padding android:left="7.0dip" android:top="7.0dip" android:right="7.0dip" android:bottom="7.0dip" />
- <corners android:radius="8.0dip" />
- </shape>
本文转自 lingdududu 51CTO博客,原文链接:
http://blog.51cto.com/liangruijun/739459