其中的技巧是在想要放置ZoomControls的位置上放置另一个Layout容器, 然后将ZoomControls插入该容器中。
真正的技巧是使用RelativeLayout
而不是LinearLayout
来定位元素,如此示例layout.xml
所示:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.google.android.maps.MapView
android:id="@+id/myMapView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:enabled="true"
android:clickable="true"
android:apiKey="MY_MAP_API_KEY"
/>
<LinearLayout android:id="@+id/layout_zoom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
layout_zoom LinearLayout元素位于屏幕底部中心,将其放置在MapView
的中下部。
然后在Activity的onCreate
中,获取对layout_zoom元素的引用,并将ZoomControl插入其中,就像您已经做的那样:
LinearLayout zoomLayout =(LinearLayout)findViewById(R.id.layout_zoom)
View zoomView = myMapView.getZoomControls()
zoomLayout.addView(zoomView, new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT))
myMapView.displayZoomControls(true)
现在,在长按操作时,ZoomControls 应该显示出来,而不会夺取地图的触摸事件。