View popView = getLayoutInflater().inflate(R.layout.message3deletepop,
null);
TextView deleTextView = (TextView) popView.findViewById(R.id.message3_delete_text);
popupWindow = new PopupWindow(popView,
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
popupWindow.setBackgroundDrawable(new PaintDrawable());
popupWindow.setFocusable(true);
popupWindow.setOutsideTouchable(true);
popupWindow.update();
显示方式3种
popupWindow.showAsDropDown(view);
popupWindow.showAsDropDown(view, xoff, yoff);
popupWindow.showAtLocation(parent, gravity, x, y);
至于作用相信大家都能通过方法名看得懂
另外:popupWindow.setBackgroundDrawable(new PaintDrawable());也可以自己设置一个透明色,这个是必须的,如果不加设置背景,则整个页面只有home键起作用,其他地方全都被屏蔽,无法点击
使用popupWindow.showAtLocation(parent, gravity, x, y);的时候如果设置了Gravity.TOP的话,top是无视状态栏高度的,所以要获取状态栏高度statusBarHeight,使y = statusBarHeight
Rect frame = new Rect();
getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
int statusBarHeight = frame.top;
状态栏高度获取要在布局完成之后,否则会是0
同时可以使用
setOnDismissListener来监听popupWindow的消失