popupwindow获取宽高

我们在确定popupwindow的时候会用到
showAtLocation(View parent, int gravity, int x, int y)
api,这个api的入參:
@param parent a parent view to get the {@link android.view.View#getWindowToken()} token from
* @param gravity the gravity which controls the placement of the popup window
* @param x the popup’s x location offset
* @param y the popup’s y location offset
第一个参数为popupwindow所附着的父视图,第二个为gravity,第三第四位window的左上角坐标。
这里gravity需要注意,如果是Gravity.letf|Gravity.top 那么window的坐标就以左上角为原点,如果是Gravity.right|Gravity.top 则是右上角为原点。

很多时候要用到popupwindow 的宽和高来精确放置它,但是经常使用popupwindow.getWidth 值为0 或者 -2。原因是普通的view都会经历Measure-》layout然后显示在window上。popupwindow是响应事件出现的,所以在它出现在屏幕之前,我们是不知道它的大小的。

解决的办法就是在初始化的时候,强制测量它填充的view:
View mContentView = LayoutInflater.from(mContext).inflate(R.layout.XXXX,null);
mContentView.measure(MeasureSpec.UNSPECIFIED,MeasureSpec.UNSPECIFIED);
PopupWindow ppw = new PopUpWindow();
ppw.setContentView(mContentView);

然后要获取ppw的宽和高,只需要ppw.getContentView().getMeasureSpecWidth()就能获取它的宽了。高度也是一样的。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值