在 Google 地图 API 片段中的方向弹出窗口上方移动浮动操作按钮?

Move floating action button above directions popup in Google Maps API fragment?

我在 Android Studio 中使用 Google 地图 API(通过默认 Google 地图 Activity)。我启用了 setMyLocationEnabled(true) 但设置了 setMyLocationButtonEnabled false,因为我想添加一个自定义浮动操作按钮,这对我来说看起来更好,可以移动到当前位置。单击标记时,API 会自动在地图片段的右下角生成指向该标记的方向按钮,但是我实现的 FAB 会覆盖这些按钮。

看了 this answer I wrapped the maps fragment and floating action button in a CoordinatorLayout, hoping that it would move the FAB up when a marker is clicked, as it does with snackbars, but it doesn't. Is there a way to activate that motion manually or at least simulate it with touch events or something similar? I tried to look for ways to move the FAB at runtime (such as ) 但我没有找到适合我的情况的解决方案。

这是 activity 布局的 xml,如果需要更多信息,请告诉我。提前致谢!

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

    <fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="(hidden for privacy)"/>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/find_my_location"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@drawable/ic_my_location_gray"
        app:backgroundTint="@color/white"/>
</android.support.design.widget.CoordinatorLayout>

我想从 xml 向你的 FAB 提供 android:layout_marginBottom 是行不通的,或者你不喜欢这个解决方案,所以:

如果您不需要它们,您可以使用 setMapToolbarEnabled() 隐藏底部的方向按钮:googleMap.getUiSettings().setMapToolbarEnabled(false);

或使用 GoogleMap.OnMarkerClickListener:

监听标记点击

googleMap.setOnMarkerClickListener(this);

然后在onMarkerClick()中做任何你想做的事:

@Override
public boolean onMarkerClick(Marker marker) {
        //give some margin programmatically
        //return false to preserve the default behaviour, true o/w 
        return true;
}