开发者

Positioning a dialog on screen

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 r开发者_运维问答elative to my overlay).


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜