片段布局内的MapView(android maps api v2)不显示 [英] MapView (android maps api v2) inside fragment layout doesnt show
问题描述
我正在使用viewpager在2个操作栏选项卡之间滑动(使用eclipse向导进行此类导航)。我正在使用android maps v2 api。
I am using viewpager to swipe between 2 action bar tabs (used eclipse wizard for this kind of navigation). I am using android maps v2 api.
我想在我的一个标签里面有mapview,button和textview(我猜想没有mapfragment)。
I want to have mapview, button and textview inside one of my tabs (i guess having mapfragment is not possible).
我从xml为我的片段扩充布局,如下所示:
I inflate layout for my fragment from xml like this:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.first_tab_fragment, container, false);
}
}
我的first_tab_fragment.xml:
My first_tab_fragment.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.google.android.gms.maps.MapView
android:id="@+id/mapview"
android:layout_width="100dip"
android:layout_height="100dip"
android:layout_alignParentTop="true"
android:layout_alignRight="@+id/textView1"
android:layout_marginRight="15dp" >
</com.google.android.gms.maps.MapView>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/textView1"
android:layout_centerHorizontal="true"
android:text="Button" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignRight="@+id/button1"
android:layout_marginBottom="133dp"
android:text="TextView" />
不显示MapView,只是textview和按钮。我也没有错。我是否必须编写一些代码来初始化mapview?
MapView is not displayed, just textview and button. I also get no errors. Do i have to write some code to initialize mapview?
推荐答案
我通过将此代码添加到onCreateView解决了这个问题:
I solved it by adding this code to onCreateView:
// Gets the MapView from the XML layout and creates it
mapView = (MapView) v.findViewById(R.id.mapview);
mapView.onCreate(savedInstanceState);
mapView.onResume(); //without this, map showed but was empty
// Gets to GoogleMap from the MapView and does initialization stuff
map = mapView.getMap();
map.getUiSettings().setMyLocationButtonEnabled(false);
map.setMyLocationEnabled(true);
// Needs to call MapsInitializer before doing any CameraUpdateFactory calls
try {
MapsInitializer.initialize(this.getActivity());
} catch (GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
}
// Updates the location and zoom of the MapView
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(44.14, 14.2), 10);
map.animateCamera(cameraUpdate);
这篇关于片段布局内的MapView(android maps api v2)不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!