读书人

Android PopupWindow运用小结

发布时间: 2013-03-21 10:08:17 作者: rapoo

Android PopupWindow使用小结
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() {@Overridepublic void onDismiss() {if(mBtnPos.isSelected()){mBtnPos.setSelected(false);resetBtnState();}}});}

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

开启or关闭:

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

?

?

读书人网 >Android

热点推荐