Android PopupWindow使用小结

先看API:http://developer.android.com/reference/android/widget/PopupWindow.html#setAnimationStyle(int)

A popup window that can be used to display an arbitrary view. The popup window is a floating container that appears on top of the current activity.

PopupWindow是一个弹出式窗口,它可以展示任意View。他会浮在当前窗口的上方展示。

常用接口例子:

private void iniPopupWindow() {
		View layout = LayoutInflater.from(mActivity).inflate(R.layout.conversation_popupwindow, null);
		mBtnMyPos = (Button) layout.findViewById(R.id.myPos);
		mBtnMapSet = (Button) layout.findViewById(R.id.mapSet);
		mBtnKeySearch = (Button) layout.findViewById(R.id.keySearch);
		mBtnCollection = (Button) layout.findViewById(R.id.collection);
		mImMyPos = (ImageView) layout.findViewById(R.id.myPosbg);
		mImMapSet = (ImageView) layout.findViewById(R.id.mapSetbg);
		mImKeySearch = (ImageView) layout.findViewById(R.id.keySearchbg);
		mImCollection = (ImageView) layout.findViewById(R.id.colbg);
		mPopupWindow = new PopupWindow(layout);
		mPopupWindow.setFocusable(true);
		mPopupWindow.setTouchable(true);
		layout.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
		mPopupWindow.setWidth(layout.getMeasuredWidth());
		mPopupWindow.setHeight((layout.getMeasuredHeight()));
//		popupwindow要有背景图片OutsideTouchable、keyback才有效
//		mPopupWindow.setBackgroundDrawable(new BitmapDrawable());
		mPopupWindow.setBackgroundDrawable(this.getResources().getDrawable(android.R.color.transparent));
//		触摸popupwindow外部,可以消失。必须设置背景
		mPopupWindow.setOutsideTouchable(true);
		mPopupWindow.setAnimationStyle(android.R.style.Animation_Toast);
		mPopupWindow.update();
		mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
			
			@Override
			public void onDismiss() {
				if(mBtnPos.isSelected()){
					mBtnPos.setSelected(false);
					resetBtnState();
				}
			}
		});
	}

 其中layout是自定义布局,PopupWindow可以任意展示,使用非常方便。

开启or关闭:

if (mPopupWindow.isShowing()) {
					mPopupWindow.dismiss();// 关闭
				} else {
					mPopupWindow.showAsDropDown(mBtnPos);// 显示
				}

 

猜你喜欢

转载自sdutdazzling.iteye.com/blog/1829842