您的位置:首页 > 运维架构

popupwindow 显示在指定的位置

2015-12-15 13:18 295 查看
需求:下面有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 源码下载
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息