View view = LayoutInflater.from(context).inflate(layout, null);
popupWindow = new PopupWindow(view, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
popupWindow.getContentView().measure(0, 0);
popHeight = popupWindow.getContentView().getMeasuredHeight();
getScreenHeight();
private void getScreenHeight() {
DisplayMetrics dm = new DisplayMetrics();
((Activity) v.getContext()).getWindowManager().getDefaultDisplay().getMetrics(dm); //v是Activity中的随意一View
mWidthPixels = dm.widthPixels;
mHeightPixels = dm.heightPixels;
}
@SuppressWarnings("deprecation")
public void showAtLocation() {
// 设置允许在外点击消失
popupWindow.setOutsideTouchable(true);
// 使其聚集
popupWindow.setFocusable(true);
// 设置弹出位置
//popupWindow.showAtLocation(v, Gravity.BOTTOM, 0, 0); //7.0以前的写法
popupWindow.showAtLocation(v, Gravity.LEFT, 0, mHeightPixels - popHeight); //7.0后的
//showDarkBg();
// 刷新状态
popupWindow.update();
}
网友评论