定位屏幕上的对话框 [英] Positioning a dialog on screen

查看:116
本文介绍了定位屏幕上的对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个图形页面的活动。我有叠加在该地图上,我想在某个项目上定位一个徘徊对话框,如果它被窃听。

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.

我已经有我做了我自己的布局和自定义背景我自己的自定义对话框。我唯一​​的问题是,如何告诉机器人在哪里(在X,Y术语)来定位对话框。

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.

这是更为复杂,因为要知道什么是该对话框的x,y。我首先需要测量后知道该对话框的宽度和高度(因为我需要计算的相对位置我覆盖了)。

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).

推荐答案

一个快速谷歌搜索想出了以下

A quick Google search came up with the following:

您可以致电getWindow()的getAttributes()来检索WindowManager.LayoutParams的窗口。这具有涉及窗口放置以下字段:

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

<一个href="http://$c$c.google.com/android/reference/android/view/ViewGroup.LayoutParams.html#width">http://$c$c.google.com/android/reference/android/view/ViewGroup.LayoutParams.html#width HTTP://$c$c.google.com/android/reference/android/view/ViewGroup.LayoutParams.html#height <一href="http://$c$c.google.com/android/reference/android/view/WindowManager.LayoutParams.html#gravity">http://$c$c.google.com/android/reference/android/view/WindowManager.LayoutParams.html#gravity HTTP://$c$c.google.com/android/reference/android/view/WindowManager.LayoutParams.html#x <一href="http://$c$c.google.com/android/reference/android/view/WindowManager.LayoutParams.html#y">http://$c$c.google.com/android/reference/android/view/WindowManager.LayoutParams.html#y

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

在做出所需的更改,使用getWindow()。setAttributes()来安装新的价值。

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

需要注意的是,虽然你可以通过width和height字段强制特定的大小,一般要做到这一点,正确的做法是让窗口做正常的布局,并自动确定窗口大小的方法。如果有一个想要一个固定的大小例如300X200的对话框在单个视图中,实施View.onMeasure()来调用setMeasuredDimension(300,200)。然后,当视图层次结构布局情况,在对话窗口会给出一个大小,确保您的看法是,尺寸(可能使实际窗口较大,考虑到对话框框架和装饰)。

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).

希望有所帮助。

这篇关于定位屏幕上的对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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