Android-特定视图上方的PopupWindow [英] Android - PopupWindow above a specific view

查看:149
本文介绍了Android-特定视图上方的PopupWindow的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Android应用程序,当用户单击屏幕底部的特定菜单栏对象(由水平排列的小图像组成)时,我正在使用弹出窗口.

I am developing an application for Android and I am using a popup window when the user clicks a specific menu bar object(consisting of small images lined up horizontally) on the bottom of the screen.

单击时,我希望将弹出窗口锚定到单击的视图的左上角并显示在顶部.

On the click I want the popup window to be anchored to the top-left corner of the view that was clicked and be shown on top.

似乎唯一相关的方法是 showAsDropDown(查看锚,int xoff,int yoff) showAtLocation(查看父级,int重力,int x,int y). showAsDropDown的问题在于它锚定在视图的左下角.

The only methods that seem to be relevant are showAsDropDown(View anchor, int xoff, int yoff) and showAtLocation(View parent, int gravity, int x, int y). The problem with showAsDropDown is that it is anchored to the bottom-left corner of the view.

还有另一种实现方法吗?

Is there another way to implement this?

推荐答案

popupWindow.showAtLocation(...)实际上显示了绝对位于屏幕上的窗口(甚至不是应用程序).该调用中的锚仅用于其窗口标记.坐标是相对于给定重力的偏移量.

popupWindow.showAtLocation(...) actually shows the window absolutely positioned on the screen (not even the application). The anchor in that call is only used for its window token. The coordinates are offsets from the given gravity.

您实际要使用的是:

popupWindow.showAsDropDown(anchor, offsetX, offsetY, gravity);

此调用仅在API 19+中可用,因此在早期版本中,您需要使用:

This call is only available in API 19+, so in earlier versions you need to use:

popupWindow.showAsDropdown(anchor, offsetX, offsetY);

这些调用显示相对于指定锚视图的弹出窗口.请注意,默认重力(在未指定重力的情况下调用时)为Gravity.TOP|Gravity.START,因此,如果您在应用的各个位置显式使用Gravity.LEFT,则可能会很麻烦:)

These calls show the popup window relative to the specified anchor view. Note that the default gravity (when calling without specified gravity) is Gravity.TOP|Gravity.START so if you are explicitly using Gravity.LEFT in various spots in your app you will have a bad time :)

这篇关于Android-特定视图上方的PopupWindow的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆