需求:下面有5个Button需要计算相应位置弹出popupwindow
1.得到button的坐标
2.得到button宽高
3.得到popupwindow宽高
4.通过上面的三个条件计算得到相应的坐标来显示得到popupwindow
得到button的坐标 //从左上角
button.getLocationInWindow(location);
int lx = location[0];
int ly = location[1];
得到button宽高
int buttonWidth = button.getWidth();int buttonHeight = button.getHeight();
int popWidth =popupwindow.getWidth();
int popHeight =popupwindow.getHeight();
计算Button1对应弹出popupwindow的位置
int x = lx; //这里x的位置就是button的坐标位置所以不用计算
int y = ly + buttonHeight
popupwindow.showAtLocation(v, Gravity.TOP|Gravity.LEFT, x, y);
int x = lx+buttonWidth-popWidth; //lx+按钮的宽度(这里时候已经到了右上角位置)再-popupwindow的宽度
int y = ly + buttonHeight
popupwindow.showAtLocation(v, Gravity.TOP|Gravity.LEFT, x, y);
int x = lx; //这里x的位置就是button的坐标位置所以不用计算
int y = ly - popHeight
popupwindow.showAtLocation(v, Gravity.TOP|Gravity.LEFT, x, y);
int x = lx +buttonWidth-popWidth; //这里和button的x计算方法是一样的
int y = ly - popHeight
popupwindow.showAtLocation(v, Gravity.TOP|Gravity.LEFT, x, y);
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 源码下载