创建菜单资源
menu_one.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/settings"
android:title="@string/menu_title_settings"></item>
<item android:id="@+id/regard"
android:title="@string/menu_title_regard"></item>
</menu>
菜单资源的使用
MainActivity.java
//重写onCreateOptionsMenu,添加一个选项菜单
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = new MenuInflater(this);
menuInflater.inflate(R.menu.menu_one,menu);
return super.onCreateOptionsMenu(menu);
}
//重写onOptionsItemSelected()方法,
// 指定各个菜单被选择时,所应做的处理
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()){
case R.id.settings:
Intent intent = new Intent(MainActivity.this,SettingsActivity.class);
startActivity(intent);
break;
case R.id.regard:
Intent intent1 = new Intent(MainActivity.this,RegardActivity.class);
startActivity(intent1);
break;
}
return super.onOptionsItemSelected(item);
}
效果: