构造方法public PopupWindow(View contentView, int width, int height, boolean focusable)
contentView为要显示的view,width和height为宽和高,值为像素值,也可以是MATCHT_PARENT和WRAP_CONTENT。
还可以public PopupWindow (Context context)
或者public PopupWindow(View contentView, int width, int height)
或者
public PopupWindow(View contentView)
其中第一种最省事,构造函数中设置了要显示的View,宽度 ,高度以及是否能获得焦点。以上是几种用的比较常见的构造方法。
改变PopupWindow的视图内容
可以通过public void setContentView(View contentView)
来改变popup的显示内容,也可以用来初始化PopupWindow的View,比如使用构造函数public PopupWindow (Context context)获得的Popupwindow就只能用setContentView来设置内容。PopupWindow popupWindow = new PopupWindow(context);
popupWindow.setContentView(contentview);
获得PopupWindow的视图内容
public View getContentView()
显示PopupWindow:
showAsDropDown(View anchor):相对某个控件的位置(正左下方),无偏移
showAsDropDown(View anchor, int xoff, int yoff):相对某个控件的位置,有偏移
showAtLocation(View parent, int gravity, int x, int y):相对于父控件的位置(例如正中央Gravity.CENTER,下方Gravity.BOTTOM等),可以设置偏移或无偏移
其实我发现showAtLocation的parent参数可以很随意,只要是activity中的view都可以。
大小:
有两种方法设置PopupWindow的大小:
调用有宽高参数的构造函数:LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View contentview = inflater.inflate(R.layout.popup_process,