关于View.getHeight和getWidth为0的解决方案(待深入学习)

来源:互联网 发布:基于梯度的分水岭算法 编辑:程序博客网 时间:2024/06/07 21:52

项目中使用到了PopupWindow填充layout然后根据点击的View以及PopupWindow中View宽高在适配的位置显示PopuWindow的问题,

但是 inflate 后的View宽高一直是得到为0,很郁闷, 但是直接设置数值是肯定不行的

popwindow.showAtLocation();

最后网上查了下用以下方法解决了


int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);

int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);

//view为填充后得到的View实例

view.measure(w, h);

//popwindow为要显示的浮动窗口

popwindow = new PopupWindow(view,view.getMeasuredWidth()
                    ,view.getMeasuredHeight(),true);

//之后再获得宽高就不为0了

popwindow.getWidth();

popwindow.getHeight();


关于MeasureSpec这个类不了解,感觉应该要深入研究下,为什么一开始得不到值设了之后就行了,但是项目太紧所以留以后再补上我的研究结果

0 0
原创粉丝点击