/** 相对于listen这个view*/
private void showPopupWindow(View listen) {
PopupWindow popupWindow;
R.layout.realtime_order_pop, null);
// 设置关闭按钮的点击事件
Button button = (Button) contentView.findViewById(R.id.order_pop_close);
button.setOnClickListener(this);//if(popupWindow != null){popupWindow.dismiss();}
popupWindow = new PopupWindow(contentView,
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true);
// 如果不设置PopupWindow的背景,无论是点击外部区域还是Back键都无法dismiss弹框
popupWindow.setBackgroundDrawable(getResources().getDrawable(
R.drawable.order_pop_bg));
popupWindow.setOutsideTouchable(false); // 如果已经.setBackgroundDrawable()则本句不起作用
popupWindow.getContentView().measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
int popupWidth = popupWindow.getContentView().getMeasuredWidth();
int popupHeight = popupWindow.getContentView().getMeasuredHeight();
// 设置好参数之后再show
int[] location = new int[2];
listen.getLocationOnScreen(location);
popupWindow.showAtLocation(listen, Gravity.NO_GRAVITY, (location[0]+listen.getWidth()/2)-popupWidth/2 , location[1]-popupHeight);
}
《!注意 布局文件 最外面不要用framelayouut 否则获取宽度可能有问题