android – Google在actionbarsherlock标签中映射

前端之家收集整理的这篇文章主要介绍了android – Google在actionbarsherlock标签中映射前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想让谷歌地图v2在我的应用程序中工作.我已经看到几个示例显示如何在活动中打开SupportMapFragment.想法是你的活动将调用setContentView(R.layout.map_layout);其中map_layout.xml链接到带有行的片段:
  1. android:name="com.google.android.gms.maps.SupportMapFragment"
  2. xmlns:map="http://schemas.android.com/apk/res-auto"

“name =”行有效地表示“此布局由”SupportMapFragment“类型的片段控制.

我的复杂之处在于我试图让地图显示在带有标签的活动中(使用actionbarsherlock实现).这意味着对应于选项卡选择的任何片段都必须实现TabListener.但是SupportMapFragment没有.所以现在我想要创建一个像这样的新片段:

  1. public class MyMapFragmentWithTabListener extends SupportMapFragment implements TabListener
  2. {

但是现在我对如何编写MapFragmentWithTabListener的内容感到困惑,特别是在onCreateView上…我应该膨胀一些布局吗?当然我不能从示例中完全膨胀相同的map_layout.xml,因为它已经声明它由SupportMapFragment控制,而在此实现中它应该由MyMapFragmentWithTabListener控制 – 我是否需要稍微不同的xml文件来膨胀(如果它应该是什么样子?) – 或者我应该以编程方式创建我的视图?

解决方法

我现在已经在很多应用程序中完成了这个.您只需创建自己的MapFragment,而不是扩展SupportMapFragment.您可以拥有自己的布局,其中包含MapView视图.关键是将Fragment的生命周期事件路由到MapView,然后让你的叔叔陷入困境.

下面是一些示例代码

MapFragment

  1. package com.example.testapplication;
  2.  
  3. import java.util.concurrent.TimeUnit;
  4.  
  5. import com.google.android.gms.common.GooglePlayServicesNotAvailableException;
  6. import com.google.android.gms.maps.GoogleMap;
  7. import com.google.android.gms.maps.MapView;
  8. import com.google.android.gms.maps.MapsInitializer;
  9.  
  10.  
  11. public class TestMapFragment extends Fragment {
  12.  
  13. private MapView mMapView;
  14.  
  15. @Override
  16. public void onActivityCreated(Bundle savedInstanceState) {
  17. super.onActivityCreated(savedInstanceState);
  18. }
  19.  
  20.  
  21. @Override
  22. public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
  23. View view = inflater.inflate(R.layout.home_fragment,container,false);
  24.  
  25. mMapView = (MapView) view.findViewById(R.id.mapview);
  26.  
  27. // inflat and return the layout
  28. mMapView.onCreate(savedInstanceState);
  29. mMapView.onResume();// needed to get the map to display immediately
  30.  
  31. try {
  32. MapsInitializer.initialize(getActivity());
  33. } catch (GooglePlayServicesNotAvailableException e) {
  34. e.printStackTrace();
  35. }
  36. GoogleMap googleMap = mMapView.getMap();
  37. googleMap.getUiSettings().setMyLocationButtonEnabled(true);
  38. return view;
  39. }
  40.  
  41. @Override
  42. public void onCreate(Bundle savedInstanceState) {
  43. super.onCreate(savedInstanceState);
  44. setRetainInstance(true);
  45. }
  46.  
  47. /*
  48. * Using a mapview in a fragment requires you to 'route'
  49. * the lifecycle events of the fragment to the mapview
  50. */
  51. @Override
  52. public void onResume() {
  53. super.onResume();
  54. if (null != mMapView)
  55. mMapView.onResume();
  56. }
  57.  
  58. @Override
  59. public void onPause() {
  60. super.onPause();
  61. if (null != mMapView)
  62. mMapView.onPause();
  63. }
  64.  
  65. @Override
  66. public void onDestroy() {
  67. super.onDestroy();
  68. if (null != mMapView)
  69. mMapView.onDestroy();
  70. }
  71.  
  72. @Override
  73. public void onSaveInstanceState(Bundle outState) {
  74. super.onSaveInstanceState(outState);
  75. if (null != mMapView)
  76. mMapView.onSaveInstanceState(outState);
  77. }
  78.  
  79. @Override
  80. public void onLowMemory() {
  81. super.onLowMemory();
  82. if (null != mMapView)
  83. mMapView.onLowMemory();
  84. }
  85. }

和布局:

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:map="http://schemas.android.com/apk/res-auto"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent" >
  5.  
  6. <com.google.android.gms.maps.MapView
  7. android:id="@+id/mapview"
  8. android:layout_width="match_parent"
  9. android:layout_height="match_parent"
  10. map:uiZoomControls="false" />
  11. </RelativeLayout>

猜你在找的Android相关文章