使用步骤基本是:
//准备PopupWindow的布局
ViewView popupView = LayoutInflater.from(this).inflate(R.layout.popup,null);
//初始化一个PopupWindow,width和height都是WRAP_CONTENT
PopupWindow popupWindow =newPopupWindow(
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//设置PopupWindow的视图内容
popupWindow.setContentView(popupView);
//点击空白区域PopupWindow消失,这里必须先设置setBackgroundDrawable,否则点击无反应popupWindow.setBackgroundDrawable(newColorDrawable(0x00000000));
popupWindow.setOutsideTouchable(true);
//设置PopupWindow动画
popupWindow.setAnimationStyle(R.style.AnimDown);
//设置是否允许PopupWindow的范围超过屏幕范围
popupWindow.setClippingEnabled(true);
//设置PopupWindow消失监听
popupWindow.setOnDismissListener(newPopupWindow.OnDismissListener() {
@OverridepublicvoidonDismiss() {
}
});
//PopupWindow在targetView下方弹出
popupWindow.showAsDropDown(targetView);
1、构造函数创建PopupWindow对象;
2、初始化一些设置(其中setWidth、setHeight和setContentView三者必须要有);
3、popupWindow.showAsDropDown(v);显示出来,或者popupWindow.showAtLocation();显示出来
如果PopupWindow中有Editor的话,focusable必须要为true。
使用setAnimationStyle方法添加动画
private void setPopBack(floatf) {//设置弹出时背景透明度
WindowManager.LayoutParams params = getWindow().getAttributes();
params.alpha= f;
getWindow().setAttributes(params);
}