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之前先進行測量!