您应该创建一个 XML 文件来定义菜单项。例如mymenu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/share"
android:icon="@drawable/ic_title_share_default"
android:title="@string/share"
android:showAsAction="ifRoom"/>
</menu>
然后在onCreateOptionsMenu
你会做:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.mymenu, menu);
return true;
}
要处理项目选择:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.share:
//do something for share
return true;
default:
return super.onOptionsItemSelected(item);
}
}
您可以在此处查看更多信息:
http://developer.android.com/guide/topics/ui/menus.html
http://actionbarsherlock.com/usage.html