如何在 MapView 上创建上下文菜单?
我想在我的 MapView
上有一个上下文菜单,让用户在背景中的地图或卫星照片之间进行选择。我尝试按照 创建菜单 创建上下文菜单,但它没有不适合我。
应用程序可以运行,但没有显示上下文菜单。如何在我的 MapView
上创建上下文菜单?
在我的 onCreate()
中,我有以下代码:
MapView mapView = (MapView) findViewById(R.id.mapview);
registerForContextMenu(mapView);
并且我重写了一些方法:
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo info) {
super.onCreateContextMenu(menu, v, info);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()) {
case R.id.map_view_item:
return true;
case R.id.satellite_item:
return true;
default: return super.onContextItemSelected(item);
}
}
I would like to have a Context Menu on my MapView
and let the user choose between a map or a satellite photo in the background. I have tried to create a Context Menu by following Creating Menus but it doesn't work for me.
The application works but no Context Menu is shown. How can I create a Context Menu on my MapView
?
In my onCreate()
I have this code:
MapView mapView = (MapView) findViewById(R.id.mapview);
registerForContextMenu(mapView);
And I have overrided some methods:
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo info) {
super.onCreateContextMenu(menu, v, info);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()) {
case R.id.map_view_item:
return true;
case R.id.satellite_item:
return true;
default: return super.onContextItemSelected(item);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看来正常的长按处理不能很好地与 MapView 配合使用,这可能是由于它处理触摸事件的方式所致。 有 一些 解决方法(如果您确实需要该功能)。
It appears that normal long-click processing does not work well with
MapView
, perhaps due to the way it handles touch events. There are some workarounds if you really need the functionality.