1.创建menu类型的xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.menudemo.MainActivity" >
<item android:id="@+id/add_items"
android:title="@string/add_name"/>
<item android:id="@+id/remove_items"
android:title="@string/remove_name"/>
</menu>
2.在Activity中绑定menu显示事件
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
3.在Activity中绑定菜单项点击事件
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.add_items:
Toast.makeText(getApplicationContext(), "add", Toast.LENGTH_LONG).show();
break;
case R.id.remove_items:
Toast.makeText(getApplicationContext(), "remove", Toast.LENGTH_LONG).show();
break;
default:
break;
}
return true;
}