在地图视图中放置缩放控件
我试图让缩放控件显示在 mapview
中,以下代码几乎可以工作,但缩放控件出现在 mapview
的左上角,而不是底部中心,就像我通过 setGravity() 指定的那样。 有人可以告诉我我缺少什么吗?
zoomView = (LinearLayout) mapView.getZoomControls();
zoomView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT));
zoomView.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL);
mapView.addView(zoomView);
这些视图/布局都是以编程方式构建的,没有需要调整的布局文件。
I'm trying to get the zoom controls to show up in a mapview
, the following code almost works, but the zoom controls appear in the top left of the mapview
, not the bottom center like I'm specifying via setGravity()
. Can someone enlighten me as to what I'm missing?
zoomView = (LinearLayout) mapView.getZoomControls();
zoomView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT));
zoomView.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL);
mapView.addView(zoomView);
These views/layouts are all constructed programmatically, there is no layout file to tweak.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
将以下行添加到
MapView
类的OnCreate()
方法中:view.setBuiltInZoomControls(true);
Add the following line to the
OnCreate()
method of yourMapView
Class:view.setBuiltInZoomControls(true);
这里的技巧是在要放置 ZoomControls 的位置放置另一个布局容器,然后将 ZoomControls 插入其中。
真正的技巧是使用
RelativeLayout
而不是LinearLayout
来定位元素,如本示例layout.xml
所示:layout_zoom LinearLayout 元素位于屏幕底部中心,将其放置在
MapView
的中间/底部。然后在您的 Activity 的 onCreate 中,获取对layout_zoom 元素的引用并将 ZoomControl 插入其中,就像您已经完成的那样:
ZoomControls 现在应该出现在长条上单击,而不窃取地图触摸事件。
The trick here is to place another Layout container where you want to put the ZoomControls and then insert the ZoomControls into that.
The real trick is to use the
RelativeLayout
rather thanLinearLayout
to position the elements, as shown in this samplelayout.xml
:The layout_zoom LinearLayout element is positioned in the bottom center of the screen, placing it over the middle/bottom of the
MapView
.Then within your Activity's
onCreate
, get a reference to the layout_zoom element and insert the ZoomControl into it, much like you've already done:The ZoomControls should now appear on a long click, without stealing the map touch events.
上面的内容对我不起作用,但这样做(将控件放在右下角):
The above didn't work for me, but this does (to place the control on the bottom right):
Reto:感谢您的回复,但我们的想法是不使用 XML 布局。
我最终解决了这个问题。 由于 MapView 是 ViewGroup 的子类,因此您可以轻松添加子视图(如缩放控件)。 您所需要的只是一个 MapView.LayoutParams 实例,然后就可以开始了。 我做了类似的事情(将缩放控件放在地图视图的底部中心)。
Reto : thanks for your reply, but the idea was to do it without using XML layouts.
I eventually worked out the problem. Because a MapView is a subclass of ViewGroup, you can easily add child views (like the zoom controls). All you need is a MapView.LayoutParams instance and you're good to go. I did something like this (puts zoom controls in the bottom center of the mapview).
从google groups thread我发现了这个:
ZoomControls without XML :
使用 SDK1.1 对我来说 100% 有效
from the google groups thread i found this:
ZoomControls without XML:
was 100% working for me with SDK1.1
不幸的是,我无法从 11 月 28 日 6:32 开始对 Jason Hudgins 批准的解决方案添加评论,但我在他的代码中遇到了一个小错误:
在这一行中:
Eclipse 给我的错误是
相反,创建一个新的 MapView.LayoutParams 对象修复了它,如下所示:
我花了一些时间才找到答案,因为我是一个 n00b :D
Unfortunately I cant add a comment to Jason Hudgins approved solution from Nov 28 at 6:32 but I got a tiny error with his code:
In this line:
The error Eclipse gave me was
instead, creating a new MapView.LayoutParams object fixed it, like this:
It took me some time to find out, as I am a n00b :D
你可以试试这个:
You can try this:
回顾 - 使用 FILL_PARENT 的问题是缩放控件会“窃取”所有触摸事件; 这样当缩放控件可见时您就无法平移地图。 你知道如何防止这种情况吗?
Reto - the problem with using FILL_PARENT is that the zoom control then "steals" all of the touch events; so that you can't pan the map while the zoom controls are visible. Do you know how to prevent this?