如何以编程方式添加地图片段

2024-02-20

我想以编程方式将此 xml 片段添加到其他片段。 是否可以?

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />

在 XML 中,您可以添加占位符容器:

<FrameLayout
        android:id="@+id/mapContainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

然后在代码中你可以这样做:

FragmentManager fm = getChildFragmentManager();
SupportMapFragment supportMapFragment =  SupportMapFragment.newInstance();
fm.beginTransaction().replace(R.id.mapContainer, supportMapFragment).commit();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何以编程方式添加地图片段 的相关文章

随机推荐