-1

我试图通过以下方式将谷歌地图放入SupportMapFragment我的MainActivity文件中:

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

附言。我删除了<fragment>标签以调试某些部分。

4

1 回答 1

0

我解决了;正如这些人在评论中教我的那样,如果有人遇到同样的情况,我将发布以供将来参考:

val mapFragment = SupportMapFragment.newInstance()
        supportFragmentManager.beginTransaction().add(R.id.map, mapFragment)
            .commit()
        mapFragment.getMapAsync(this)

谢谢你。

于 2020-02-18T22:03:56.803 回答