java 片段布局内的 MapView(android maps api v2)不显示

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14582110/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 16:45:27  来源:igfitidea点击:

MapView (android maps api v2) inside fragment layout doesnt show

javaandroidandroid-fragmentsandroid-viewpagerandroid-maps-v2

提问by DixieFlatline

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.

我正在使用 viewpager 在 2 个操作栏选项卡之间滑动(使用 eclipse 向导进行这种导航)。我正在使用 android maps v2 api。

I want to have mapview, button and textview inside one of my tabs (i guess having mapfragment is not possible).

我想在我的一个选项卡中包含 mapview、button 和 textview(我想不可能有 mapfragment)。

I inflate layout for my fragment from xml like this:

我从 xml 中为我的片段膨胀布局,如下所示:

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);
    }
}

My first_tab_fragment.xml:

我的 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 is not displayed, just textview and button. I also get no errors. Do i have to write some code to initialize mapview?

MapView 不显示,只显示文本视图和按钮。我也没有收到任何错误。我是否必须编写一些代码来初始化 mapview?

回答by DixieFlatline

I solved it by adding this code to onCreateView:

我通过将此代码添加到 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);