参考:http://www.cnblogs.com/salam/archive/2011/04/04/2005329.html博主,并进行补充。
关于OptionsMenu的例子,博主“永恒的记忆”的文章http://www.cnblogs.com/salam/archive/2011/04/04/2005329.html已经讲的非常清楚并且举了非常好的例子,这里只是补充一下关于上下文菜单(ContextMenu)和子菜单(SubMenu)。
关于ContextMenu的详细介绍请参考:http://developer.android.com/reference/android/view/ContextMenu.html。在这里主要强调API当中这一点:
To show a context menu on long click, most clients will want to call registerForContextMenu(View)
and overrideonCreateContextMenu(ContextMenu, View, ContextMenu.ContextMenuInfo)
.
就是说为了显示一个在长按View时显示ContextMenu,多数客户端都会调用registerForContextMenu(View)并且复写onCreateContextMenu(ContextMenu, View, ContextMenu.ContextMenuInfo)方法。在此处registerForContextMenu(View)方法写在onCreate当中。
在实际开发当中用于长按触发ContextMenu的方法还有设置View的setOnCreateContextMenuListener监听事件。
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) ;但长按View,要显示上下文菜单时调用产生上下文菜单。
public boolean onContextItemSelected(MenuItem item); 选中菜单项后发生的动作。
public void onContextMenuClosed(Menu menu);菜单关闭后发生的动作。