【发布时间】:2014-01-10 01:12:43
【问题描述】:
我正在使用 ActionBarSherlock 在 Android 2.2 上显示碎片标签 我遇到的问题是我无法在 Google 地图片段上显示我的自定义 OptionsMenu。我设置了 setHasOptionsMenu(true);在 onCreate 方法中,但它仍然没有出现。
这是我的谷歌地图fragment3.java
public class Fragment3 extends SherlockFragment {
private MapView mMapView;
private int group1Id = 1;
int homeId = Menu.FIRST;
int profileId = Menu.FIRST +1;
int searchId = Menu.FIRST +2;
int dealsId = Menu.FIRST +3;
int helpId = Menu.FIRST +4;
int contactusId = Menu.FIRST +5;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment3, container, false);
mMapView = (MapView) view.findViewById(R.id.mapview);
// inflat and return the layout
mMapView.onCreate(savedInstanceState);
mMapView.onResume();// needed to get the map to display immediately
try {
MapsInitializer.initialize(getActivity());
} catch (GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
}
GoogleMap googleMap = mMapView.getMap();
googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
googleMap.moveCamera( CameraUpdateFactory.newLatLngZoom(new LatLng(54.902815,23.9500459) , 16.0f) );
// googleMap.setMyLocationEnabled(true);
googleMap.getUiSettings().setZoomControlsEnabled(true);
return view;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
setHasOptionsMenu(true);
}
/*
* Using a mapview in a fragment requires you to 'route'
* the lifecycle events of the fragment to the mapview
*/
@Override
public void onResume() {
super.onResume();
if (null != mMapView)
mMapView.onResume();
}
@Override
public void onPause() {
super.onPause();
if (null != mMapView)
mMapView.onPause();
}
@Override
public void onDestroy() {
super.onDestroy();
if (null != mMapView)
mMapView.onDestroy();
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
if (null != mMapView)
mMapView.onSaveInstanceState(outState);
}
@Override
public void onLowMemory() {
super.onLowMemory();
if (null != mMapView)
mMapView.onLowMemory();
}
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(group1Id, homeId, homeId, "").setIcon(R.drawable.action_about);
menu.add(group1Id, profileId, profileId, "").setIcon(R.drawable.action_about);
menu.add(group1Id, searchId, searchId, "").setIcon(R.drawable.action_about);
menu.add(group1Id, dealsId, dealsId, "").setIcon(R.drawable.action_about);
menu.add(group1Id, helpId, helpId, "").setIcon(R.drawable.action_about);
menu.add(group1Id, contactusId, contactusId, "").setIcon(R.drawable.action_about);
return onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 1:
........
case 6:
//code here
default:
break;
}
return super.onOptionsItemSelected(item);
}
}
它没有出现是因为它在一个片段中吗?有什么方法可以在谷歌地图片段中启用我自己的自定义菜单? 欢迎大家回答和讨论, 如果您需要查看我的任何处理片段或 .xml 文件的适配器类,请告诉我。
【问题讨论】:
标签: android google-maps android-fragments actionbarsherlock android-optionsmenu