PopupWindow在创建时宽度高度设置为match_parent或者wrap_content时,通过getWidth、getHeight或者getContentView.getMeasuredWidth、getContentView.getMeasuredHeight 不能获取到真实的高度!
正确的方法获取高度的方法是创建之后调用measure方法对View进行测量,然后获取宽度与高度!
示例:
- View popupWindowView = View.inflate(getContext(), R.layout.popupwindow_layout, null);
- PopupWindow popupWindow=new PopupWindow(popupWindowView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
- popupWindow.getContentView().measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
- popupWindow.setBackgroundDrawable(new ColorDrawable(0));
- int popHeight=popupWindow.getContentView().getMeasuredHeight();
- popupWindow.showAsDropDown(view, 0, -popHeight);
其他弹出类的窗口类似!在获取width与height之前先进行测量!