popupwindow 怎么显示在 view 上面
时间: 2024-01-19 08:00:52 浏览: 169
要将 PopupWindow 显示在 View 上面,首先需要获取 View 的坐标位置,然后根据坐标位置计算 PopupWindow 的显示位置。
首先可以通过 View 的 getLocationOnScreen(int[] location) 方法获取 View 在屏幕上的坐标位置,location 数组中的第一个元素是 View 的横坐标,第二个元素是纵坐标。
然后可以调用 PopupWindow 的 showAtLocation(View parent, int gravity, int x, int y) 方法来显示PopupWindow,其中parent参数是要显示在其上面的View,gravity是Gravity的一个常量,用来定义PopupWindow的位置,x和y是PopupWindow在屏幕上的偏移量。
通常来说,可以通过获取View的坐标位置,然后根据需要计算PopupWindow的显示位置,然后调用showAtLocation方法来显示PopupWindow。另外,也可以使用showAsDropDown(View anchor)方法来显示PopupWindow,该方法会将PopupWindow显示在View的下方。
总之,通过获取View的坐标位置,并利用PopupWindow的showAtLocation或showAsDropDown方法,就可以将PopupWindow显示在View的上方或下方了。
阅读全文
相关推荐













