什么是PopupWindow?

谷歌的描述是:“一个弹出窗口控件,可以用来显示任意视图(View),而且会浮动在当前 活动(activity)的顶部”

下面展示一个最简单的实例

安卓 <wbr>简单的PopupWindow使用实例填写图片摘要(选填)

点击button时,弹出PopupWindow

public void showPopupWindow(View view) {
LayoutInflater inflater=LayoutInflater.from(this);
final View popView=inflater.inflate(R.layout.popupwindow_layout, null);//创建PopupWindow的内容视图
Button dismiss_bt= (Button) popView.findViewById(R.id.dismiss_bt);
final PopupWindow popupWindow=new PopupWindow(popView, ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT, true);//创建PopupWindow
popupWindow.setTouchable(true);
popupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.ba));//设置popupwindow的背景
dismiss_bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "button is pressed", Toast.LENGTH_SHORT).show();
popupWindow.dismiss();
}
});
popupWindow.showAsDropDown(view);//显示PopupWindow
}
PopupWindow的显示方法
  显示提供了两种形式:
  使用showAtLocation()显示在指定位置,两个方法重载:
public void showAtLocation(View parent, int gravity, int x, int y)

public void showAtLocation(IBinder token, int gravity, int x, int y)
 
  使用showAsDropDown()显示在一个参照物View的周围,三个方法重载:
public void showAsDropDown(View anchor)

public void showAsDropDown(View anchor, int xoff, int yoff)

public void showAsDropDown(View anchor, int xoff, int yoff, int gravity)
public void showAsDropDown(View anchor, int xoff, int yoff, int gravity)方法是API 19新引入的。
PopupWindow如果没有设置Background在按下Back键或点击PopupWindow外区域的时候不会Dismiss。
原因:如果有背景,则会在contentView外面包一层PopupViewContainer之后作为mPopupView,如果没有背景,则直接用contentView作为mPopupView。

而这个PopupViewContainer是一个内部私有类,它继承了FrameLayout,在其中重写了Key和Touch事件的分发处理。由于PopupView本身并没有重写Key和Touch事件的处理,所以如果没有包这个外层容器类,点击Back键或者外部区域是不会导致弹框消失的。