如何从带有map的膨胀布局中获取SupportMapFragment

时间:2017-11-30 22:36:03

标签: android google-maps android-layout google-maps-markers android-inflate

从膨胀的布局

获取SupportMapFragment时遇到问题

我在另一个布局上有一张地图,但是我应该如何使用getSupportFragmentManager()?

这是我的MainActivity.java

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //...

    LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View vi = inflater.inflate(R.layout.content_main, null);

    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);

    //...

}

和content_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

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

</LinearLayout>

谢谢你的回答!

0 个答案:

没有答案