View v = getLayoutInflater().inflate(R.layout.pop_video_resolution, null);
mPopupWindow = new PopupWindow(v, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
......
View popupView = mPopupWindow.getContentView();
// 创建一个临时的父视图来测量popupView
ViewGroup tempParent = new FrameLayout(this);
tempParent.addView(popupView);
// 强制测量和布局popupView
popupView.measure(
View.MeasureSpec.makeMeasureSpec(ViewGroup.LayoutParams.MATCH_PARENT, View.MeasureSpec.AT_MOST),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)
);
popupView.layout(0, 0, popupView.getMeasuredWidth(), popupView.getMeasuredHeight());
// 现在你可以获取popupView的高度
int popupHeight = popupView.getMeasuredHeight();
// 移除popupView从临时父视图
tempParent.removeView(popupView);
// 弹出弹窗,因为是预测量实际可能会有差异,若有可自行补一点差
int[] location = new int[2];
mVBg.getLocationOnScreen(location);
mPopupWindow.showAtLocation(mVBg,Gravity.NO_GRAVITY,0,location[1]-popupHeight);
Android的PopupWindow弹窗高度为WRAP_CONTENT时贴边显示在指定view的上面
最新推荐文章于 2024-08-25 16:26:29 发布