在res/menu下建立一个menu画布:
mune.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/help"
android:title="帮助"
/>
<item android:id="@+id/our"
android:title="联系我们"/>
</menu>
public class MainActivity extends Activity {
private static final int OK=1;
private static final int CANCLE=2;
private static final int ABOUT=3;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onPrepareOptionsMenu(Menu menu)
{
menu.add(0, OK, 0, "开始");
menu.add(0, CANCLE, 0, "取消");
Menu file=menu.addSubMenu(0,ABOUT,0,"关于");
MenuInflater inflater=getMenuInflater();
inflater.inflate(R.menu.mune, file);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch(item.getItemId())
{
case OK:
this.setTitle("开始");
return true;
case CANCLE:
this.setTitle("取消");
return true;
case ABOUT:
this.setTitle("关于");
return true;
case R.id.help:
this.setTitle("帮助信息");
return true;
case R.id.our:
this.setTitle("联系我们");
return true;
}
return false;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}