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 源码下载
思路
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 源码下载
相关文章推荐
- android教程之使用popupwindow创建菜单示例
- android PopupWindow 和 Activity弹出窗口实现方式
- Android PopupWindow 点击外面取消实现代码
- android自定义popupwindow仿微信右上角弹出菜单效果
- Android PopupWindow使用实例
- Android中PopupWindow响应返回键并关闭的2种方法
- android使用PopupWindow实现页面点击顶部弹出下拉菜单
- Android入门之PopupWindow用法实例解析
- android popwindow实现左侧弹出菜单层及PopupWindow主要方法介绍
- 一段android跳转到web界面的代码
- 收藏各种技术源码
- PopupWindow gridview 无法响应Item
- PopupWindow 实现 弹出后 其余部分变灰(类似dialog)
- 一个简单的PopupWindow做的选项菜单点击按钮会在按钮下弹出几个下拉控件
- PopupWindow的使用
- PopupWindow(第一篇CSDN博客,希望能坚持写下去,也希望大家多多支持)
- PopupWindow的显示位置及简单的响应事件
- popupwindow上使用spinner,能显示,但当点击spinner时出现异常
- PopupWindow
- popupwindow 弹出框 使用详情