PopupWindow弹出动态计算展示的位置,左、右、上、下对齐
来源:互联网 发布:阿里云日本服务器 编辑:程序博客网 时间:2024/05/21 14:44
PopupWindow弹出动态计算展示的位置,左、右、上、下对齐
- 附上完整代码
public class PopupWindowUtil { /** * 计算出来的位置,y方向就在anchorView的上面和下面对齐显示,x方向就是与屏幕右边对齐显示 * 如果anchorView的位置有变化,就可以适当自己额外加入偏移来修正 * * @param anchorView 呼出window的view * @param contentView window的内容布局 * @return window显示的左上角的xOff, yOff坐标 */ public static int[] calculatePopWindowPos(final View anchorView, final View contentView) { final int windowPos[] = new int[2]; final int anchorLoc[] = new int[2]; // 获取锚点View在屏幕上的左上角坐标位置 anchorView.getLocationOnScreen(anchorLoc); final int anchorHeight = anchorView.getHeight(); final int anchorPaddingTop = anchorView.getPaddingTop(); // 获取屏幕的高宽 final int screenHeight = UIUtil.getScreenHeight(); final int screenWidth = UIUtil.getScreenWidth(); // 测量contentView contentView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED); // 计算contentView的高宽 final int windowHeight = contentView.getMeasuredHeight(); final int windowWidth = contentView.getMeasuredWidth(); // 判断需要向上弹出还是向下弹出显示 final boolean isNeedShowUp = (screenHeight - anchorLoc[1] - anchorHeight < windowHeight); if (isNeedShowUp) { windowPos[0] = screenWidth - windowWidth;//向下 windowPos[1] = anchorLoc[1] - windowHeight + anchorHeight - anchorPaddingTop; } else { windowPos[0] = screenWidth - windowWidth;//向上 windowPos[1] = anchorLoc[1] + anchorHeight - anchorHeight + anchorPaddingTop; } return windowPos; } /** * pop自动调整位置显示在左右两侧 * * @param anchorView 呼出window的view * @param contentView window的内容布局 * @return window显示的左上角的xOff, yOff坐标 */ public static int[] calculatePopWindow(final View anchorView, final View contentView) { final int windowPos[] = new int[2]; final int anchorLoc[] = new int[2]; // 获取锚点View在屏幕上的左上角坐标位置 anchorView.getLocationOnScreen(anchorLoc); final int anchorHeight = anchorView.getHeight(); final int anchorWidth = anchorView.getWidth(); final int anchorPaddingTop = anchorView.getPaddingTop(); final int anchorPaddingLeft = anchorView.getPaddingLeft(); // 获取屏幕的高宽 final int screenHeight = UIUtil.getScreenHeight(); final int screenWidth = UIUtil.getScreenWidth(); // 测量contentView contentView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED); // 计算contentView的高宽 final int windowHeight = contentView.getMeasuredHeight(); final int windowWidth = contentView.getMeasuredWidth(); // 判断需要向上弹出还是向下弹出显示 final boolean isNeedShowUp = (screenHeight - anchorLoc[1] - anchorHeight < windowHeight); if (isNeedShowUp) { windowPos[0] = screenWidth - windowWidth; windowPos[1] = anchorLoc[1] - windowHeight + anchorHeight - anchorPaddingTop; } else { windowPos[0] = screenWidth - windowWidth; windowPos[1] = anchorLoc[1] + anchorHeight - anchorHeight + anchorPaddingTop; } boolean isRight = (screenWidth - anchorLoc[0] - anchorWidth > windowWidth); int xOff = UIUtil.dip2px(20);//偏移量 if (isRight) { windowPos[0] = anchorLoc[0] + anchorWidth + anchorPaddingLeft + xOff;//右 } else { windowPos[0] = anchorLoc[0] - windowWidth - anchorPaddingLeft - xOff;//左 } return windowPos; }}
初始化PopupWindow
int windowPos[] = PopupWindowUtil.calculatePopWindowPos(iv, view);int xOff = UIUtil.dip2px(50);windowPos[0] -= xOff;popWindow.showAtLocation(iv, Gravity.TOP | Gravity.START, windowPos[0], windowPos[1]);
阅读全文
0 0
- PopupWindow弹出动态计算展示的位置,左、右、上、下对齐
- PopupWindow的弹出位置
- PopupWindow如何右对齐
- 实现字符串的对齐(左对齐、右对齐、居中)
- table的左/右/上/下边框处理
- 如何隐藏table的左/右/上/下边框
- Android手势(上,下,左和右的判断)
- Android手势(上,下,左和右的判断)
- 如何隐藏table的左/右/上/下边框
- Android手势(上,下,左和右的判断)
- popwindow在View的上,下,左,右 显示
- 在Button上、下、左、右位置添加图片和文字
- 左对齐和右对齐
- PopupWindow展示位置
- 实现TStringGrid的左对齐、居中和右对齐
- ADC左对齐与右对齐的数据读取问题
- 左对齐、右对齐的9X9乘法表01
- c++左对齐/右对齐的头文件
- 分布式、集群和微服务概念整理
- Android 在一个activity中关闭其他activity
- elastic-job部署以及简单例子
- MongoDB数据库之数据的导入与导出
- 第一章 Shiro简介——《跟我学Shiro》
- PopupWindow弹出动态计算展示的位置,左、右、上、下对齐
- shiro身份认证(第一章)
- qt 中获取鼠标位置
- zeit/micro Error -- Cannot find module '../lib/help'
- IPTV工作原理
- 关于ArcGis的.geodatabase离线地图不能正确显示中文标记的解决办法
- [剑指offer]面试题10:二进制中1的个数
- 使用TCP协议编写一个网络程序,设置服务器程序监听端口为8002,当与客户端建立连接后,向客户端发送“hello world”,客户端接负责将信息输出
- Makefile十二之make的运行