在屏幕上定位对话框

发布于 2024-10-08 16:06:58 字数 213 浏览 0 评论 0原文

我有一个关于 MapView 的活动。我在该地图上有叠加层,如果点击某个项目,我想在该项目上放置一个悬停对话框。

我已经有了自己的自定义对话框,我用自己的布局和自定义背景组成了。我唯一的问题是如何告诉 Android 在哪里(以 x、y 表示)放置对话框。

更复杂的是要知道对话框 x, y 是什么。我首先需要知道测量后对话框的宽度和高度(因为我需要它来计算相对于叠加层的位置)。

I have an activity with MapView. I have overlays on that map and I would like to position a hovering dialog over an item if it is tapped.

I already have my own custom dialog I've made up with my own layout and custom background. My only problem is how to tell Android where (in x, y terms) to position the dialog.

It's even more complicated since to know what's the dialog x, y. I first need to know the dialog's width and height after measurement (since I need that to calculate the position relative to my overlay).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暖风昔人 2024-10-15 16:06:58

通过 Google 快速搜索,得到了以下内容

您可以调用 getWindow().getAttributes() 来检索窗口的 WindowManager.LayoutParams。其中包含以下涉及窗口放置的字段:

http:// code.google.com/android/reference/android/view/ViewGroup.LayoutParams.html#width http://code.google.com/android/reference/android/view/ViewGroup.LayoutParams.html#height http://code.google.com/android/reference/android/view/WindowManager.LayoutParams。 html#重力
http://code.google.com/android/reference /android/view/WindowManager.LayoutParams.html#x http://code.google.com/android/reference/android/view/WindowManager.LayoutParams.html#y

进行所需的更改后,使用 getWindow().setAttributes() 安装新的价值观。

请注意,虽然您可以通过宽度和高度字段强制指定特定大小,但通常正确的方法是让窗口执行其正常布局并以这种方式自动确定窗口大小。如果对话框中有一个视图需要固定大小(例如 300x200),请实现 View.onMeasure() 来调用 setMeasuredDimension(300, 200)。然后,当视图层次结构布局发生时,对话框窗口将被指定一个大小,以确保您的视图是该尺寸(可能使实际窗口更大以考虑对话框框架和装饰)。

希望有帮助。

A quick Google search came up with the following:

You can call getWindow().getAttributes() to retrieve the WindowManager.LayoutParams for the window. This has the following fields involving window placement:

http://code.google.com/android/reference/android/view/ViewGroup.LayoutParams.html#width http://code.google.com/android/reference/android/view/ViewGroup.LayoutParams.html#height http://code.google.com/android/reference/android/view/WindowManager.LayoutParams.html#gravity
http://code.google.com/android/reference/android/view/WindowManager.LayoutParams.html#x http://code.google.com/android/reference/android/view/WindowManager.LayoutParams.html#y

After making your desired changes, use getWindow().setAttributes() to install the new values.

Note that, though you can force a specific size through the width and height fields, in general the correct way to do this is let the window do its normal layout and determine the window size automatically that way. If you have a single view in the dialog that wants a fixed size such as 300x200, implement View.onMeasure() to call setMeasuredDimension(300, 200). Then when the view hierarchy layout happens, the dialog window will be given a size that ensures your view is that dimension (probably making the actual window larger to take into account the dialog frame and decoration).

Hope that helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文