将小部件添加到Google Maps Fragment Android

时间:2015-08-12 10:34:52

标签: android google-maps android-fragments android-widget

我设置了默认的Google地图项目。

Main Activity中,引用的布局为activity_maps.xml

此布局包含一个名为map的片段,该片段也在MainActivity中引用。

我想在我假设的片段中添加小部件到这个布局,可能是一个用于选择地图区域的下拉菜单。

我该怎么做?

当我尝试渲染activity_maps时,我得到一个空白屏幕,因为之前我将片段引用到activity_maps并导致StackOverflow ...哎呀!

所以,在我们解决了这个问题之后(我尝试刷新),我仍然认为我不能简单地添加小部件,是吗?

2 个答案:

答案 0 :(得分:0)

我将这些小部件与地图小部件(即不同的片段)分开,但是将它们放在我的activity_maps.xml中以使它们连接(即同时FrameLayout或{{ 1}})。或者如果你想在一个片段中,那么方法将是相同的。将您想要的任何小部件放在布局的单独部分中,并用它覆盖地图。

答案 1 :(得分:0)

我使用的是Android Studio 2.3。要从默认的Google地图项目向布局文件添加小部件,可能需要将布局转换为约束布局。然而,这样做会导致

注意:这是一个kludge,但它对我有用。

步骤1)带有地图片段的默认xml文件。除非将布局转换为约束格式,否则不允许添加小部件。

<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="com.mycompany.MyActivity" />

步骤2)将布局转换为约束布局。根据需要添加MapView和其他小部件。

<android.support.constraint.ConstraintLayout 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="com.mycompany.MyActivity" >

<com.google.android.gms.maps.MapView
    android:id="@+id/mapView"
    android:layout_width="0dp"
    android:layout_height="0dp"
    tools:layout_constraintTop_creator="1"
    tools:layout_constraintRight_creator="1"
    tools:layout_constraintBottom_creator="1"
    android:layout_marginStart="2dp"
    map:layout_constraintBottom_toBottomOf="parent"
    android:layout_marginEnd="2dp"
    map:layout_constraintRight_toRightOf="parent"
    android:layout_marginTop="8dp"
    tools:layout_constraintLeft_creator="1"
    android:layout_marginBottom="8dp"
    map:layout_constraintLeft_toLeftOf="parent"
    map:layout_constraintTop_toTopOf="parent"
    android:layout_marginLeft="2dp"
    android:layout_marginRight="2dp" />

<Button
    android:id="@+id/button3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button"
    tools:layout_constraintRight_creator="1"
    tools:layout_constraintBottom_creator="1"
    map:layout_constraintBottom_toBottomOf="parent"
    android:layout_marginEnd="137dp"
    map:layout_constraintRight_toRightOf="parent"
    android:layout_marginBottom="157dp"
    android:layout_marginRight="137dp" />

步骤3:从步骤2编辑xml。使MapView成为片段

<android.support.constraint.ConstraintLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.myCompany.MyActivity" >

<fragment
    android:id="@+id/mapView"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="0dp"
    android:layout_height="0dp"
    tools:layout_constraintTop_creator="1"
    tools:layout_constraintRight_creator="1"
    tools:layout_constraintBottom_creator="1"
    android:layout_marginStart="2dp"
    map:layout_constraintBottom_toBottomOf="parent"
    android:layout_marginEnd="2dp"
    map:layout_constraintRight_toRightOf="parent"
    android:layout_marginTop="8dp"
    tools:layout_constraintLeft_creator="1"
    android:layout_marginBottom="8dp"
    map:layout_constraintLeft_toLeftOf="parent"
    map:layout_constraintTop_toTopOf="parent"
    android:layout_marginLeft="2dp"
    android:layout_marginRight="2dp" />

<Button
    android:id="@+id/button3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button"
    tools:layout_constraintRight_creator="1"
    tools:layout_constraintBottom_creator="1"
    map:layout_constraintBottom_toBottomOf="parent"
    android:layout_marginEnd="137dp"
    map:layout_constraintRight_toRightOf="parent"
    android:layout_marginBottom="157dp"
    android:layout_marginRight="137dp" />