【问题标题】:How to add a button and overlay it on my Google map fragment如何添加按钮并将其覆盖在我的 Google 地图片段上
【发布时间】:2019-01-10 15:52:28
【问题描述】:

所以我有一个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 手机视图时,它只是说,我不确定如何在此地图顶部添加按钮,如果可能的话,甚至在其下方添加按钮。 我知道ConstraintLayoutFrameLayout,但是在添加其他代码时,我只是得到“多根错误”并且无法实现我想要的。

谁能帮帮我?

【问题讨论】:

    标签: android xml android-studio button google-api


    【解决方案1】:

    这里是约束布局的例子

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout 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"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        tools:context=".MapsActivity">
    
        <fragment
            android:id="@+id/map"
            android:name="com.google.android.gms.maps.SupportMapFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            />
    
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="Button"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            android:layout_margin="16dp"
    
            />
    
    </android.support.constraint.ConstraintLayout>
    

    【讨论】:

      【解决方案2】:

      也许更简单的解决方案是使用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>
      

      【讨论】:

        【解决方案3】:

        您必须使用FrameLayoutLinearLayoutRelativeLayoutConstraintLayout 包装您的片段,然后设置其他按钮。

        而不是这个:

        <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" />
        

        应该是这样的:

        <?xml version="1.0" encoding="utf-8"?>
        <RelativeLayout 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">
        
        <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"/>
        
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_alignParentBottom="true"
            android:layout_margin="15dp"
            tools:text="Button one"/>
        
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentBottom="true"
            android:layout_margin="15dp"
            tools:text="Button two"/>
        
         </RelativeLayout>
        

        这只是RelativeLayout 的示例。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-05-15
          • 2013-05-14
          • 1970-01-01
          • 1970-01-01
          • 2014-09-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多