【问题标题】:Move floating action button above directions popup in Google Maps API fragment?在 Google Maps API 片段中的方向弹出上方移动浮动操作按钮?
【发布时间】:2016-12-08 14:23:12
【问题描述】:

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

查看this answer 之后,我将地图片段和浮动操作按钮包裹在CoordinatorLayout 中,希望在单击标记时它会向上移动FAB,就像对小吃店所做的那样,但事实并非如此。有没有办法手动激活那个动作,或者至少用触摸事件或类似的东西来模拟它?我试图寻找在运行时移动 FAB 的方法(例如 this),但未能找到适合我的情况的解决方案。

这是活动布局的 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>

【问题讨论】:

    标签: android android-fragments google-maps-android-api-2 android-coordinatorlayout floating-action-button


    【解决方案1】:

    我想从 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;
    }
    

    【讨论】:

    • 虽然我没有使用您的确切解决方案,但提到 onMarkerClick 让我想按照您的建议在其中做点什么。实际上,我已经实现了为每个标记加载一些数据,所以我只是在onMarkerClick() 上添加了FAB.hide(),在onMapClick() 上添加了FAB.show()(因为可能不需要使用信息窗口向上)。我不知道为什么我之前没有想到,但还是谢谢你!
    猜你喜欢
    • 2016-08-26
    • 2023-04-07
    • 1970-01-01
    • 1970-01-01
    • 2019-09-15
    • 2015-06-25
    • 1970-01-01
    • 1970-01-01
    • 2019-06-07
    相关资源
    最近更新 更多