Android 如何使用 setBuiltInZoomControls(true) 布局缩放控件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1768097/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to layout zoom Control with setBuiltInZoomControls(true)?
提问by Mak Sing
would like to add a zoom control to the map. I also want to layout the position of the zoom Control instead of the default middle bottom position. I can do this by getZoomControl but it is deprecated.
想向地图添加缩放控件。我还想布局缩放控件的位置而不是默认的中间底部位置。我可以通过 getZoomControl 做到这一点,但它已被弃用。
Could anyone tell me how to do this with setBuildtInZoomControls
?
谁能告诉我如何做到这一点setBuildtInZoomControls
?
回答by duskstriker
This is how I got mine working finally (by embedding a ZoomControl in XML layout).
这就是我最终工作的方式(通过在 XML 布局中嵌入 ZoomControl)。
mapView = (MapView) this.findViewById(R.id.mapView);
ZoomControls zoomControls = (ZoomControls) findViewById(R.id.zoomcontrols);
zoomControls.setOnZoomInClickListener(new OnClickListener() {
public void onClick(View v) {
mapView.getController().zoomIn();
}
});
zoomControls.setOnZoomOutClickListener(new OnClickListener() {
public void onClick(View v) {
mapView.getController().zoomOut();
}
});
回答by user1634451
LinearLayout zoomLayout = (LinearLayout) findViewById(R.id.layout_zoom);
View mapController = mapView.getZoomButtonsController().getZoomControls();
zoomLayout.addView(mapController, new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
回答by slup
mapView.getZoomButtonsController()
Although this is undocumented (at least in the javadoc available here: com.google.android.maps) I am quite sure this is the replacement for the deprecated getZoomControls
尽管这是未记录的(至少在此处提供的 javadoc 中:com.google.android.maps)我很确定这是已弃用的替代品getZoomControls
Edit: just found out that it isdocumented, just not in the google api docs but rather here: ZoomButtonsController on developer.android.com
编辑:刚刚发现它被记录在案,只是不在 google api 文档中,而是在这里:developer.android.com 上的 ZoomButtonsController
You could then call getContainer ()
or getZoomControls ()
(depending on what you want to do) to get access to the zoom controls view.
然后您可以调用getContainer ()
或getZoomControls ()
(取决于您想要做什么)来访问缩放控件视图。
And then do something like
然后做类似的事情
RelativeLayout.LayoutParams zoomParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
zoomParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
mapView.getZoomButtonsController().getZoomControls().setLayoutParams(zoomParams);
although I have to admit that I am not sure what layout the maps view uses, could be LinearLayout as well.
虽然我不得不承认我不确定地图视图使用什么布局,但也可能是 LinearLayout。
回答by gantzer89
The solution provided by Georgeon Placing Zoom Controls in a MapViewallows to layout the built in zoom controls by using gravity attributes. This attribute positions the element into its container which seems that is positioned on the bottom of the screen so its not possible to position the zoom controls on the top of the screen by layouting the zoom controls.
所提供的解决方案乔治就在MapView配售缩放控制允许通过重力属性布局内置的缩放控件。此属性将元素定位到其容器中,该容器似乎位于屏幕底部,因此无法通过布局缩放控件将缩放控件定位在屏幕顶部。
In conclusion you can layout the built in zoom controls by doing:
总之,您可以通过执行以下操作来布局内置缩放控件:
((FrameLayout.LayoutParams) mapView.getZoomButtonsController().getZoomControls().getLayoutParams()).gravity = Gravity.RIGHT;
Remember that the zoom controls are contained into a FrameLayout
therefore any try to use rules like RelativeLayout.ALIGN_PARENT_BOTTOM
or RelativeLayout.ALIGN_PARENT_TOP
will lead you only to pain and sorrow.
请记住,缩放控件包含在一个FrameLayout
因此任何尝试使用规则RelativeLayout.ALIGN_PARENT_BOTTOM
或RelativeLayout.ALIGN_PARENT_TOP
将只会导致您痛苦和悲伤。
回答by Jeremy Logan
I'm pretty sure that the only way to do this is to either use the MapView's deprecated getZoomControls()or to DIY it. The MapControllerhas the methods necessary (e.g. zoomIn()and zoomOut()).
我很确定要做到这一点的唯一方法是使用MapView已弃用的getZoomControls()或 DIY 它。该MapController具有必要的方法(例如zoomIn()和缩小(ZoomOut)() )。
回答by Flash
RelativeLayout relativeLayout = new RelativeLayout(this);
setContentView(relativeLayout);
final MapView mapView = new MapView(this, DEBUG_MAP_API_KEY);
RelativeLayout.LayoutParams mapViewLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,RelativeLayout.LayoutParams.FILL_PARENT );
relativeLayout.addView(mapView, mapViewLayoutParams);
RelativeLayout.LayoutParams zoomControlsLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT );
zoomControlsLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
zoomControlsLayoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
relativeLayout.addView(mapView.getZoomControls(), zoomControlsLayoutParams);
mapView.setClickable(true);
mapView.setEnabled(true);
I think this should work ... Hope this is useful :)
我认为这应该有效......希望这是有用的:)