如何显示PopupWindow在特殊的位置? [英] How to show PopupWindow at special location?
本文介绍了如何显示PopupWindow在特殊的位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要证明 PopupWindow
在一个浏览
显示在屏幕上。
I need to show PopupWindow
under one Views
shown on the screen.
我怎么能计算出所需的查看
和地点 PopupWindow
下它的坐标? code的例子非常欢迎。谢谢你。
How can I calculate coordinates of needed View
and place PopupWindow
under it? Code example are more than welcome. Thanks.
推荐答案
查找一个已经显示的视图是相当容易 - 这是我在我的code使用:
Locating an already displayed view is fairly easy - here's what I use in my code:
public static Rect locateView(View v)
{
int[] loc_int = new int[2];
if (v == null) return null;
try
{
v.getLocationOnScreen(loc_int);
} catch (NullPointerException npe)
{
//Happens when the view doesn't exist on screen anymore.
return null;
}
Rect location = new Rect();
location.left = loc_int[0];
location.top = loc_int[1];
location.right = location.left + v.getWidth();
location.bottom = location.top + v.getHeight();
return location;
}
您可以再使用code类似于Ernesta建议粘在相关位置弹出:
You could then use code similar to what Ernesta suggested to stick the popup in the relevant location:
popup.showAtLocation(parent, Gravity.TOP|Gravity.LEFT, location.left, location.bottom);
这将直接显示在弹出的原始视图下 - 不能保证会有足够的空间,虽然显示视图
This would show the popup directly under the original view - no guarantee that there would be enough room to display the view though.
这篇关于如何显示PopupWindow在特殊的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文