当PopupWindow的动画样式animationStyle被设置后,需要在显示PopupWindow之前调用PopupWindow的setAnimationStyle()方法才能生效。另外,为了确保PopupWindow的宽度和高度都已被正确地测量,请在调用showAsDropDown()或showAtLocation()方法前调用PopupWindow的setHeight()和setWidth()方法设置布局参数。以下是一个示例代码:
PopupWindow popupWindow = new PopupWindow(context);
View contentView = LayoutInflater.from(context).inflate(R.layout.popup_layout, null);
popupWindow.setContentView(contentView);
popupWindow.setOutsideTouchable(true);
popupWindow.setFocusable(true);
popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
//设置动画样式
popupWindow.setAnimationStyle(R.style.popup_window_animation);
//设置布局参数
popupWindow.setHeight(LayoutParams.WRAP_CONTENT);
popupWindow.setWidth(LayoutParams.WRAP_CONTENT);
//显示PopupWindow
popupWindow.showAsDropDown(anchorView);