首页 > 解决方案 > 如何添加按钮并将其覆盖在我的 Google 地图片段上

问题描述

所以我有自己GoogleMap使用我的 API 密钥。我可以找到我的位置,但打开应用程序时没有任何按钮。如果我尝试通过 android 设计布局单击并拖动任何按钮,则不允许这样做。

这就是我的activity_maps.xml样子:

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MapsActivity" />

当查看它旁边的 android 手机视图时,它只是说,我不确定如何在这张地图的顶部添加一个按钮,如果可能的话,甚至在它的下方添加一个按钮。我知道ConstraintLayoutand FrameLayout,但是在添加其他代码时,我只是得到“多根错误”并且无法实现我想要的。

谁能帮帮我?

标签: androidxmlandroid-studiobuttongoogle-api

解决方案


也许一个更简单的解决方案是在您的地图前面设置一个叠加层,使用FrameLayoutRelativeLayout将它们视为您活动中的常规按钮。您应该以从后到前的顺序声明您的图层,例如,在按钮之前映射,尝试以下布局,看看它是否适合您

 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MapActivity" >

        <fragment xmlns:map="http://schemas.android.com/apk/res-auto"
            android:id="@+id/map"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:scrollbars="vertical"  
            class="com.google.android.gms.maps.SupportMapFragment"/>

        <Button
            android:id="@+id/btn"
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:orientation="horizontal" 
            android:background="#80000000"
            android:padding="4dp" >
    </FrameLayout>

推荐阅读