final PopupWindow window = mPageStatWin;
if(null != window && window.isShowing()) {
win.dismiss();
}
window.showAtLocation(parent, Gravity.RIGHT | Gravity.BOTTOM, 10,10);
//将PopupWindow作为anchor的下拉窗口显示。即在anchor的左下角显示
window.showAsDropDown(anchor);
//xoff,yoff基于anchor的左下角进行偏移。
window.showAsDropDown(anchor, xoff, yoff);
//创建一个包含自定义view的PopupWindow
private PopupWindow makePopupWindow(Context cx) {
PopupWindow window;
window = new PopupWindow(cx);
TextView contentView = new TextView(cx);
contentView.setGravity(Gravity.CENTER);
final Resources res = cx.getResources();
// contentView.setBackgroundColor(R.color.page_window_bgcolor);
// window.setBackgroundDrawable(new
// ColorDrawable(res.getColor(R.color.page_window_bgcolor)));
contentView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
//设置PopupWindow显示和隐藏时的动画
window.setAnimationStyle(R.style.AnimationFade);
//设置PopupWindow的大小(宽度和高度)
window.setWidth(res.getDimensionPixelSize(R.dimen.page_window_width));
window.setHeight(res.getDimensionPixelSize(R.dimen.page_window_height));
//设置PopupWindow的内容view
window.setContentView(contentView);
//设置PopupWindow外部区域是否可触摸
window.setOutsideTouchable(true);
return window;
}
<!-- PopupWindow窗口淡入淡出动画 -->
<style name="AnimationFade">
<item name="android:windowEnterAnimation">@anim/fade_in</item>
<item name="android:windowExitAnimation">@anim/fade_out</item>
</style>
window.setAnimationStyle(R.style.AnimationFade);
window.setFocusable(true); //设置PopupWindow可获得焦点
window.setTouchable(true); //设置PopupWindow可触摸
window.setOutsideTouchable(true); //设置非PopupWindow区域可触摸
window.showAtLocation();
window.showAsDropDown();