将小部件添加到 Google 地图片段 Android
Adding widgets to Google Maps Fragment Android
我设置了一个默认的 Google 地图项目。
在Main Activity
中,被引用的布局是activity_maps.xml
此布局包含一个名为 map
的片段,它也在 MainActivity
中引用。
我想在这个布局中添加小部件,在我推测的片段中,可能是一个用于在地图上选择区域的下拉菜单。
我该怎么做?
当我尝试呈现 activity_maps
时出现空白屏幕,因为之前我将片段引用到 activity_maps
并导致了 Whosebug...哎呀!
所以,在我们解决了这个问题之后(我尝试刷新),我仍然认为我不能简单地添加小部件,可以吗?
我会将这些小部件与地图小部件分开(即不同的片段),但将它们放在我的 activity_maps.xml
中以使它们连接(即两者在相同的 FrameLayout
或 RelativeLayout
).或者,如果您希望将其放在一个片段中,则方法是相同的。将您想要的任何小部件放在布局的单独部分,并用它覆盖您的地图。
我使用的是 Android Studio 2.3。要将小部件从默认 Google 地图项目添加到布局文件,可能需要将布局转换为约束布局。然而,这样做会导致
注意:这是一个拼凑,但对我有用。
步骤 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" />
我设置了一个默认的 Google 地图项目。
在Main Activity
中,被引用的布局是activity_maps.xml
此布局包含一个名为 map
的片段,它也在 MainActivity
中引用。
我想在这个布局中添加小部件,在我推测的片段中,可能是一个用于在地图上选择区域的下拉菜单。
我该怎么做?
当我尝试呈现 activity_maps
时出现空白屏幕,因为之前我将片段引用到 activity_maps
并导致了 Whosebug...哎呀!
所以,在我们解决了这个问题之后(我尝试刷新),我仍然认为我不能简单地添加小部件,可以吗?
我会将这些小部件与地图小部件分开(即不同的片段),但将它们放在我的 activity_maps.xml
中以使它们连接(即两者在相同的 FrameLayout
或 RelativeLayout
).或者,如果您希望将其放在一个片段中,则方法是相同的。将您想要的任何小部件放在布局的单独部分,并用它覆盖您的地图。
我使用的是 Android Studio 2.3。要将小部件从默认 Google 地图项目添加到布局文件,可能需要将布局转换为约束布局。然而,这样做会导致
注意:这是一个拼凑,但对我有用。
步骤 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" />