我正在使用Google Maps API for Android。当我点击一个标记时,以下代码可以在右下角启用地图工具栏:
googleMap.getUiSettings().setMapToolbarEnabled(true);
然而,我希望这个地图工具栏出现在地图的右上角,而不是默认的右下角位置。您该怎么做呢?
我正在使用Google Maps API for Android。当我点击一个标记时,以下代码可以在右下角启用地图工具栏:
googleMap.getUiSettings().setMapToolbarEnabled(true);
我之前遇到过一个关于如何将工具栏移到页面底部的答案,希望这能帮到你。
//get reference to my location icon
View locationButton = ((View) mapFragment.getView().findViewById(Integer.parseInt("1")).
getParent()).findViewById(Integer.parseInt("2"));
// and next place it, for example, on bottom right (as Google Maps app)
RelativeLayout.LayoutParams rlp = (RelativeLayout.LayoutParams) locationButton.getLayoutParams();
// position on right bottom
rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0);
rlp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
rlp.setMargins(0, 0, 30, 30);
编辑: 根据 @Andro 的建议,这是更改地图工具栏位置的代码:
//获取到我的位置图标的引用
View toolbar = ((View) mapFragment.getView().findViewById(Integer.parseInt("1")).
getParent()).findViewById(Integer.parseInt("4"));
// and next place it, for example, on bottom right (as Google Maps app)
RelativeLayout.LayoutParams rlp = (RelativeLayout.LayoutParams) toolbar.getLayoutParams();
// position on right bottom
rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0);
rlp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
rlp.setMargins(0, 0, 30, 30);
View locationButton = ((View) mapFragment.getView().findViewById(Integer.parseInt("1")). getParent()).findViewById(Integer.parseInt("4"));
- AndroparseInt("4")
? - MetaSnarf虽然我想发表评论,但是我的声望很低,所以:
据我所知,目前还没有定位它的方法。它只会出现在默认位置。
如Nathan所说,如果可能的话,我应该添加一些替代方案。所以,有一个变通方法:您可以禁用地图的工具栏,并在布局中的任何位置添加appcompact的ToolBar。
参考: https://developer.android.com/reference/android/support/v7/widget/Toolbar.html
2021年更新
你可以通过设置googleMap对象的padding来移动地图界面控件(指南针、我的位置、缩放等)。这是我发现的仅有的提供内部方式。以下是官方文档中的相关信息:
当设计与地图某些部分重叠的用户界面时,Padding可以很有帮助。例如,在下面的图片中,地图沿着顶部和右侧边缘进行了填充。可见的地图控件和法律文本将显示在填充区域的边缘。
https://developers.google.com/maps/documentation/android-sdk/map#map_padding