(1) 通过构造函数创建popWindow对象,构造器规定了展示的View
(2) 可以通过showAsDropDown()展示,此时是显示在某控件的下方,还可以通过showAtLocation()展示,此方法可以设置显示的位置
(3) 关闭通过dismiss()方法关闭
(4) 若希望点击外部让popWindow消失则需要让Activity从onPause状态变为可用的状态(弹窗,PopWindow展示都会使Activity进入暂停状态),可以通过设置popupWindow.setOutsideTouchable(true);,并设置window.setFocusable(false);这样就可以点击外部了,然后就可以在onTouchEvent中添加dismiss方法了,记得判断popWindow是否为null并判断是否处于展示状态。
例子
private voidsetPopWindow() {
Viewview = getLayoutInflater().inflate(R.layout.listview_show,null);//pop展示的界面
Buttonsure = (Button) view.findViewById(R.id.sure);
Buttonnosure = (Button) view.findViewById(R.id.nosure);
sure.setOnClickListener(this);
nosure.setOnClickListener(this);
ListViewlistview = (ListView) view.findViewById(R.id.showdata);
listview.setAdapter(newMyAdapter());
WindowManagerm = getWindowManager();
Displayd = m.getDefaultDisplay(); // 为获取屏幕宽、高
window= new PopupWindow(view, d.getWidth() / 3*2, d.getHeight()/ 2);
window.showAsDropDown(ceshiSpinner,0, 4);//显示
}
摘录自:http://blog.csdn.net/harvic880925/article/details/49278705
PopWindow的setFocusable(boolean focusable):当设置为false时则pop上的控件都不能获取焦点,若pop上有输入框则输入框不能输入文字,所以我们pop上若希望获取焦点则需要设置true.
PopWindow上的setTouchable(boolean touchable):当设置为false时则pop上的控件就没有作用了。