PopupWindow显示在某个控件上方

View contentView = LayoutInflater.from(mContext).inflate(R.layout.popup_chat_layout, null);
//我这里给的是固定高度  固定高度需要在外边设置  在布局中设置外边不设置是没有效果的
final PopupWindow mPopWindow1 = new PopupWindow(contentView,ViewGroup.LayoutParams.WRAP_CONTENT, 130, true);
mPopWindow1.setBackgroundDrawable(new BitmapDrawable());
// 设置弹窗外可点击
mPopWindow1.setOutsideTouchable(true);
mPopWindow1.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
mPopWindow1.setHeight(130);
mPopWindow1.setContentView(contentView);
//获取自身的长宽高
contentView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
popupHeight = contentView.getMeasuredHeight();
popupWidth = contentView.getMeasuredWidth();
//获取需要在其上方显示的控件的位置信息
int[] location = new int[2];
v.getLocationOnScreen(location);
//在控件上方显示    向上移动y轴是负数   
mPopWindow1.showAtLocation(v, Gravity.NO_GRAVITY, (location[0] + v.getWidth() / 2) - popupWidth / 2, location[1] - popupHeight-70);

猜你喜欢

转载自blog.csdn.net/mr___xu/article/details/80706796