PopupWindow的使用

在做Menu菜单时,系统为我们提供的Menu菜单在实际开发时,并不能满足我们的要求,于是想起来了PopupWindow,SDK API是对样对PopupWindow定义的:A popup window that can be used to display an arbitrary view. The popup windows is a floating container that appears on top of the current activity.  大概意思是这样:这是一个可以显示任意的View的弹出窗口,这个窗口是浮动在当前的Activity上的,

<!--StartFragment -->

 

下面就用一个小例子来展示下PopupWindow的用法吧!下面就用一个Menu菜单作为例子,我个人比较喜欢用XML文件,所以下面都以XML为主

首先来写Menu菜单的布局 menu_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:id="@+id/ll" android:orientation="vertical" android:background="@drawable/menu_bg"
	android:layout_width="fill_parent" android:layout_height="wrap_content" android:focusableInTouchMode="true">
	<LinearLayout android:id="@+id/item_top"
		android:layout_width="fill_parent" android:layout_height="wrap_content"
		android:orientation="horizontal" android:layout_weight="4">
		<LinearLayout android:id="@+id/item1_1" android:background="@drawable/menu_item_bg"
			android:layout_width="wrap_content" android:layout_height="wrap_content"
			android:orientation="vertical" android:gravity="center"
			android:layout_weight="1">
			<ImageView android:id="@+id/imge" android:layout_width="wrap_content"
				android:layout_height="wrap_content" android:src="@drawable/icon"></ImageView>
			<TextView android:layout_width="wrap_content"
				android:layout_height="wrap_content" android:text="btn1" />
		</LinearLayout>
		<LinearLayout android:id="@+id/item1_2" android:background="@drawable/menu_item_bg"
			android:layout_width="wrap_content" android:layout_height="wrap_content"
			android:orientation="vertical" android:gravity="center"
			android:layout_weight="1">
			<ImageView android:id="@+id/imge" android:layout_width="wrap_content"
				android:layout_height="wrap_content" android:src="@drawable/icon"></ImageView>
			<TextView android:layout_width="wrap_content"
				android:layout_height="wrap_content" android:text="btn1" />
		</LinearLayout>
		<LinearLayout android:id="@+id/item1_3" android:background="@drawable/menu_item_bg"
			android:layout_width="wrap_content" android:layout_height="wrap_content"
			android:orientation="vertical" android:gravity="center"
			android:layout_weight="1">
			<ImageView android:id="@+id/imge" android:layout_width="wrap_content"
				android:layout_height="wrap_content" android:src="@drawable/icon"></ImageView>
			<TextView android:layout_width="wrap_content"
				android:layout_height="wrap_content" android:text="btn1" />
		</LinearLayout>
		<LinearLayout android:id="@+id/item1_4" android:background="@drawable/menu_item_bg"
			android:layout_width="wrap_content" android:layout_height="wrap_content"
			android:orientation="vertical" android:gravity="center"
			android:layout_weight="1">
			<ImageView android:id="@+id/imge" android:layout_width="wrap_content"
				android:layout_height="wrap_content" android:src="@drawable/icon"></ImageView>
			<TextView android:layout_width="wrap_content"
				android:layout_height="wrap_content" android:text="btn1" />
		</LinearLayout>
	</LinearLayout>
</LinearLayout>

 它的样式只是显示一排,这一拓为四个按钮,为了让例子更加清楚,我把其它的代码都删了

 

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:background="@color/good">
	<GridView android:id="@+id/gridView2" android:layout_width="fill_parent"
		android:layout_height="fill_parent" android:focusableInTouchMode="true" />
</LinearLayout>

 这里的布局不会影响到具体的操作,所以我也没有怎么改。

 

下面的代码才是关键

 

package com.hilary.dialog;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import com.hilary.R;

public class PopupWindowActivity extends Activity {
	public static PopupWindow popupWindow;

	private LinearLayout linear1_1;
	private LinearLayout linear1_2;
	private LinearLayout linear1_3;
	private LinearLayout linear1_4;

	View menuView = null;
	View parent = null;
	GridView gridView;
	LinearLayout linear;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.popup_window);
		initWeight();
		initListener();
	}

	private void initWeight() {
		menuView = PopupWindowActivity.this.getLayoutInflater().inflate(
				R.layout.menu_list, null);
		parent = getLayoutInflater().inflate(R.layout.popup_window, null);
		popupWindow = new PopupWindow(menuView, LayoutParams.FILL_PARENT,
				LayoutParams.WRAP_CONTENT, false);

		/*
		 * 设置此项为true时,会让其这个popupWindow处于焦点,其它控件(除主页面键)都会
		 * 失去焦点,不可点击
		 * popupWindow.setFocusable(true);	
		 */
		
		/*
		 * 有的人说让下面这两项设计结合可以让PopupWindow失去焦点从而实现点击别的控件时,
		 * PopupWindow会消失,这样虽然是可以实现这样的效果,但有一点不足之处就是,这样他
		 * 会重构你点击那项的背景,当你与点击selector并用时,就是产生你点击那项,它的内
		 * 容会消失(在你重写按钮背景时),也许是因为那项的背景重构的事吧。。所以大家千万不要这样做
		 * 
		 * popupWindow.setOutsideTouchable(true);
		 * popupWindow.setBackgroundDrawable(new BitmapDrawable());
		 * 
		 */
		popupWindow.setAnimationStyle(R.style.MenuAnimation);
		/*
		gridView = (GridView) findViewById(R.id.gridView);
		linear1_1 = (LinearLayout) menuView.findViewById(R.id.item1_1);
		linear1_2 = (LinearLayout) menuView.findViewById(R.id.item1_2);
		linear1_3 = (LinearLayout) menuView.findViewById(R.id.item1_3);
		linear1_4 = (LinearLayout) menuView.findViewById(R.id.item1_4);
	}

	private void initListener() {
		linear1_1.setOnClickListener(new ClickEvent());
		linear1_2.setOnClickListener(new ClickEvent());
		linear1_3.setOnClickListener(new ClickEvent());
		linear1_4.setOnClickListener(new ClickEvent());
		
		gridView.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if(popupWindow != null && popupWindow.isShowing()){
					_setDismiss();
				}
				return false;
			}
		});
		
	}

	public void getPopupWindow() {
		if (popupWindow == null) {
			_showingPopupWindw();
		} else {
			if (popupWindow.isShowing()) {
				_setDismiss();
			} else {
				_showingPopupWindw();
			}
		}
	}

	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_MENU) {
			if (popupWindow != null) {
				if (popupWindow.isShowing()) {
					_setDismiss();
				} else {
					getPopupWindow();
				}
			} else {
				getPopupWindow();
			}
		} else if (keyCode == KeyEvent.KEYCODE_BACK) {
			if(popupWindow != null && popupWindow.isShowing()){
				_setDismiss();
			}else{
				System.exit(0);
			}
		}
		return false;
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		System.out.println("onKeyDown");
		return false;
	}

	private class ClickEvent implements OnClickListener {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.btn1:
				getPopupWindow();
				break;
			case R.id.btn2:
				getPopupWindow();
				break;
			case R.id.item1_1:
				System.out.println("item1_1");
				break;
			case R.id.item1_2:
				System.out.println("item1_2");
				break;
			case R.id.item1_3:
				System.out.println("item1_3");
				break;
			case R.id.item1_4:
				System.out.println("item1_4");
				break;
			}
			popupWindow.dismiss();
		}

	}
	
	public void _setDismiss() {
		 popupWindow.dismiss();
	}
	
	public void _showingPopupWindw(){
		popupWindow.showAtLocation(parent, Gravity.BOTTOM, 0, 0);
	}
}
	

 在我们用PopupWindow时,都会与某种布局结合应用,如我上面的应用,UC上的应用是与WebView应用,所以在点击外面时,PopupWindow消失,要在WebView上添加Touch监听事件,如果在整个布局上添加Touch是不成效的

你可能感兴趣的:(android)