引用转载的一段话:
AlertDialog是非阻塞线程的,而PopupWindow是阻塞线程的
PopupWindow的位置按照有无偏移分,可以分为偏移和无偏移两种;按照参照物的不同,可以分为相对于某个控件(Anchor锚)和相对于父控件。
具体如下:
showAsDropDown(View anchor):相对某个控件的位置(正左下方),无偏移
showAsDropDown(View anchor, int xoff, int yoff):相对某个控件的位置,有偏移
showAtLocation(View parent, int gravity, int x, int y):相对于父控件的位置(例如正中央Gravity.CENTER,下方Gravity.BOTTOM等),可以设置偏移或无偏移”
其实,PopupWindow很简单
android.widget.PopupWindow.PopupWindow(View contentView, int width,int height)
Create a new non focusable popup window which can display the contentView. The dimension of the window must be passed to this constructor.
The popup does not provide any background. This should be handled by the content view.
-
Parameters:
- contentView the popup's content
- width the popup's width
- height the popup's height
看到这里大概就知道怎样用了,
直接new一个PopupWindow就行了
第一步:
PopupWindow popupWindow = new PopupWindow(popupView,width,heigh);
三个参数,第一个参数是要显示的文本,通常是xml布局,通过
View popupView = LayoutInflater.from(this).inflate(R.layout.home,null);
找到,然后第二个参数是PopupWindow的宽,第三个参数是高
如果想自动适配,可以这样:
popupWindow = new PopupWindow(popupView, LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
第二步:
show:
popupWindow.showAsDropDown(v);
参数:the view on which to pin the popup window
注意问题:
http://stackoverflow.com/questions/8753291/androidlistview-in-popupwindow-cannot-be-selected
popupWindow.setFocusable(true);// 使其聚集
popupWindow.setOutsideTouchable(true);// 设置允许在外点击消失
popupWindow.update();//刷新状态(必须刷新否则无效)
但是这样点击popuWindow 以外的区域还是不能够关闭它
必须再添加以下代码:
popupWindow.setBackgroundDrawable(new BitmapDrawable());