popupwindow 显示在指定的位置

需求:下面有5个Button需要计算相应位置弹出popupwindow


思路
1.得到button的坐标
2.得到button宽高
3.得到popupwindow宽高
4.通过上面的三个条件计算得到相应的坐标来显示得到popupwindow


得到button的坐标 //从左上角


    int[] location = new int[2];
    button.getLocationInWindow(location); 
    int lx = location[0]; 

    int ly = location[1];

得到button宽高

   int buttonWidth = button.getWidth();

   int buttonHeight = button.getHeight();


得到popupwindow宽高
   int popWidth =popupwindow.getWidth();

   int popHeight =popupwindow.getHeight();


注意这里计算是从左上角开始算的Gravity.TOP|Gravity.LEFT
计算Button1对应弹出popupwindow的位置
   int x = lx;  //这里x的位置就是button的坐标位置所以不用计算
   int y = ly + buttonHeight 

   popupwindow.showAtLocation(v, Gravity.TOP|Gravity.LEFT, x, y);


计算Button2对应弹出popupwindow的位置
   int x = lx+buttonWidth-popWidth;  //lx+按钮的宽度(这里时候已经到了右上角位置)再-popupwindow的宽度
   int y = ly + buttonHeight

   popupwindow.showAtLocation(v, Gravity.TOP|Gravity.LEFT, x, y);


计算Button3对应弹出popupwindow的位置
   int x = lx;  //这里x的位置就是button的坐标位置所以不用计算
   int y = ly - popHeight

   popupwindow.showAtLocation(v, Gravity.TOP|Gravity.LEFT, x, y);


计算Button4对应弹出popupwindow的位置
   int x = lx +buttonWidth-popWidth;  //这里和button的x计算方法是一样的
   int y = ly - popHeight

   popupwindow.showAtLocation(v, Gravity.TOP|Gravity.LEFT, x, y);


计算Button5对应弹出popupwindow的位置
   int x = lx +buttonWidth-popWidth;  //这里和button的x计算方法是一样的
   int y = ly + buttonHeight

   popupwindow.showAtLocation(v, Gravity.TOP|Gravity.LEFT, x, y);


以上有什么问题欢迎吐槽!

http://download.csdn.net/detail/lipingaccp/9357311 源码下载



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值