PopupWindow showAtLocation位置显示不对的处理

原因是控件的getMeasuredHeigh和getMeasuredWidth获取值不对。

需要示的view还没有画出来的时候是无法获取到它的宽高的。

解决方法:

方案1:宽度高度写死

方案2:调用两次showAtLocation 第一次让他显示在屏幕外,这样再去拿到正确的宽高,dimiss后再调用showAtLocation,这样就显示对了。代码如下

final View contentView = LayoutInflater.from(this).inflate(R.layout.picture_quality_option, null);
if (pictureQualityWindow == null) {
 
    pictureQualityWindow = new PopupWindow(contentView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    pictureQualityWindow.setOutsideTouchable(false);
    pictureQualityWindow.setTouchable(true);
}
 
pictureQualityWindow.showAtLocation(bottomView, Gravity.NO_GRAVITY, 0, 0);//先显示再屏幕外
contentView.post(new Runnable() {
    @Override
    public void run() {
        int height = contentView.getMeasuredHeight();
        pictureQualityWindow.dismiss();//隐藏
        pictureQualityWindow.showAsDropDown(bottomView, 0, -(bottomView.getHeight() + height));//显示正确的布局
    }
});

你可能感兴趣的:(android,UI,android,PopupWindow)