单击按钮后如何加载Mapfragment

时间:2014-07-11 08:09:35

标签: android google-maps google-maps-api-3 android-fragments google-maps-android-api-2

我有一个MainActivity扩展ActionBarAcitivity引用main_screen_layout.xml

它还有一个抽屉和一个操作栏。 我在这个活动中有一个按钮。我点击此按钮后想加载地图。

对于地图,我有一个包含

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

和Mapfragment.java类扩展Fragment引用mapfragment.xml Mapfragment.java实现了onCreateView方法。

现在我将这个片段添加到运行时的mainactivity中。这工作正常。 但是如何操作我活动中片段内的地图呢?

2 个答案:

答案 0 :(得分:0)

  1. 获取附加在活动
  2. 上的片段
  3. E.G。 Fragment frag = getFragmentManager()。findFragmentById / Tag(id / tag);
  4. 获取片段的根视图
  5. E.G。查看root = frag.getView();
  6. 找到你的目标视图来操纵它,就像在一个活动中一样。
  7. E.G。查看target = root.findViewById(id);

答案 1 :(得分:0)

使用mapView更简单。

将你的片段xml放入:

<com.google.android.gms.maps.MapView
        android:id="@+id/mapId"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
</com.google.android.gms.maps.MapView>

并在你的java片段类中:

MapView mapView = (MapView) getActivity().findViewById(R.id.mapId); //get view

GoogleMap map = mapView.getMap(); //working with map

mapView.setVisibility(View.GONE); //to make disappear map
mapView.setVisibility(View.VISIBLE); // to make appear map