实现popupwindow 首先继承PopupWindow
在子类的构造方法:public GroupListPopupWindow(Context context) 方法中
一定要添加:
// 添加菜单视图
this.setContentView(mRootView);//
// 不设置width和height显示不出来
this.setWidth(LayoutParams.WRAP_CONTENT);
this.setHeight(LayoutParams.WRAP_CONTENT);
this.setFocusable(true);// menu菜单获得焦点 如果没有获得焦点menu菜单中的控件事件无法响应
this.setBackgroundDrawable(new BitmapDrawable());// http://blog.youkuaiyun.com/hustpzb/article/details/7891803,没有这个点击外面关不了popupwindow
显示时,调用
mGroupListPopupWindow.showAsDropDown(layoutInsideCenter, 0, 0);
会将layoutInsidecenter的左下角作为popupwindow的左上角锚点,popupwindow在layoutInsidecenter下面显示。后面的两个值是x方向和y方向的偏移,注意单位是dp!