Android 获取PopupWindow宽高为0

Android 获取PopupWindow宽高为0

一、环境

JDK8 + Android API23

二、问题

需要将PopupWindow展示在页面某个元素(假设为 sampleButton)的上方,所以 showAtLocation Y方向的值应该是 sampleButton.getTop() - popupWindow.getHeight()

但是打断点发现 getHeight 获取到值为0

三、解决

这是由于当元素不可见时,获取到的都是0,应该换种方式,使用**getMeasuredHeight()**获取真实高度

// 解决在View不可见时,直接getHeight为0的问题
int size[] = new int[2];
int width = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
int height = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
popupWindow.getContentView().measure(width, height);
size[0] = popupWindow.getContentView().getMeasuredWidth();
size[1] = popupWindow.getContentView().getMeasuredHeight();
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值