使用PopupWindow有以下两步:
1. 调用PopupWindow的构造器创建PopupWindow对象。
2. 调用PopupWindow的showAsDropDown(View v)将PopupWindow作为v组件的下拉组件显示出来,调用PopupWindow的showAtLocation(View v,int Gravity,intx,iny y)将PopupWindow显示在指定位置。
PopupWindow的使用例子代码如下:
// 加载界面布局文件
final View root = MainActivity.this.getLayoutInflater().inflate(R.layout.my_view,null);
// 为PopupWindow指定界面和宽高
final PopupWindow popupWindow = new PopupWindow(root,560,700);
// popWindow作为v组件的下拉组件显示出来
popupWindow.showAsDropDown(v);
// 设置PopupWindow显示的位置
popupWindow.showAtLocation(popWindow, Gravity.CENTER,50,50);
root.findViewById(R.id.close).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 关闭popUpWindow
popupWindow.dismiss();
}
});
其中R.layout.my_view作为PopupWindow的内容界面,里面有一个id为close的按钮来控制PopupWindow的关闭