【发布时间】:2020-02-18 01:09:58
【问题描述】:
我试图将谷歌地图放入我的MainActivity 文件中的SupportMapFragment,如下所示:
val mapFragment = supportFragmentManager.findFragmentById(R.id.map) as? SupportMapFragment
mapFragment?.getMapAsync(this)
和
val mapFragment = supportFragmentManager
.findFragmentById(R.id.map) as SupportMapFragment
mapFragment.getMapAsync(this)
并修改我的content_main.xml 喜欢:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="center_vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/app_name"
android:textColor="#FFFFFF"
style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"/>
</LinearLayout>
和
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="13"
android:orientation="horizontal"
android:gravity="center_vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/app_name"
android:textColor="#FFFFFF"
style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"/>
</LinearLayout>
有什么建议吗?
编辑
这是我的Activity_Main.xml:
<include
xmlns:tools="http://schemas.android.com/tools"
android:name="com.google.android.gms.maps.SupportMapFragment"
layout="@layout/content_main"
android:id="@+id/map"
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"
/>-->
ps。我删除了<fragment>标签来调试一些部分。
【问题讨论】:
-
什么时候使用 FragmentTransaction 将 MapFragment 添加到 FragmentManager?
-
代码与XML最不匹配,因为没有
R.id.map。 -
你能检查一下我上次编辑的内容吗?谢谢。
-
@EpicPandaForce 我是一个新开发的安卓应用程序。我对此不太了解。
-
@Mareyes 为什么要包含布局而不是片段?
标签: android kotlin supportmapfragment child-fragment