Popwindow 没有继承Viewgroup,因此最外层布局属性的宽高无效,如果布局设置wrap_content则以实际资源大小为准,例如:给布局定义背景图片,如果图片是.9格式要特别注意,设置了宽高后会默认加上没有被拉升的部分宽高。
Popwindow 显示方法有两种,即显示在某个view下和显示在窗体中的某个xy位置,如果设置了比重,Gravity.END|Gravity.TOP 此时从上到下,从右到左的偏移量为正。
public class Pop extends PopupWindow {
private View conentView;
public Pop(final Activity context) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
conentView = inflater.inflate(R.layout.pop_task, null);
int h = context.getWindowManager().getDefaultDisplay().getHeight();
int w = context.getWindowManager().getDefaultDisplay().getWidth();
// 设置SelectPicPopupWindow的View
this.setContentView(conentView);
// 设置SelectPicPopupWindow弹出窗体的宽
this.setWidth((int)context.getResources().getDimension(R.dimen.px_to_dp_232));
// 设置SelectPicPopupWindow弹出窗体的高
this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
// 设置SelectPicPopupWindow弹出窗体可点击
this.setFocusable(true);
this.setOutsideTouchable(true);
// 刷新状态
this.update();
// 实例化一个ColorDrawable颜色为半透明
ColorDrawable dw = new ColorDrawable(0000000000);
// 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ,设置其他控件变化等操作
this.setBackgroundDrawable(dw);
}