Eug*_*ene 27 android popupwindow
我需要在屏幕上显示的PopupWindow一个下面Views显示.
如何计算所需坐标View和位置PopupWindow?代码示例非常受欢迎.谢谢.
zee*_*ker 106
找到已经显示的视图相当容易 - 这是我在代码中使用的内容:
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;
}
Run Code Online (Sandbox Code Playgroud)
然后,您可以使用类似于Ernesta建议的代码将弹出窗口粘贴到相关位置:
popup.showAtLocation(parent, Gravity.TOP|Gravity.LEFT, location.left, location.bottom);
Run Code Online (Sandbox Code Playgroud)
这将直接在原始视图下显示弹出窗口 - 不能保证有足够的空间来显示视图.
你有getLeft()并且getBottom()在布局中获得视图的确切位置.你还必须getWidth()和getHeight()知道通过视图所占用的确切空间.如果要将弹出窗口放在视图下方.
您setLeft()和setTop()视图的方法来定位新的弹出窗口.
| 归档时间: |
|
| 查看次数: |
52277 次 |
| 最近记录: |