import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class mainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// 使用XML创建菜单
// MenuInflater inflater = getMenuInflater();
// // 设置menu界面
// inflater.inflate(R.menu.menu, menu);
// 使用代码添加菜单
menu.add(0, 0, 0, R.string.app_name);
return true;
}
// 处理菜单事件
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int itemid = item.getItemId();
switch(itemid) {
case R.id.i1:
break;
default:
break;
}
return true;
}
/**
* 打开菜单
*/
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
return super.onPrepareOptionsMenu(menu);
}
/**
* 关闭菜单
*/
@Override
public void onOptionsMenuClosed(Menu menu) {
// TODO Auto-generated method stub
super.onOptionsMenuClosed(menu);
}
}
菜单XML
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<group
android:id="@+id/g1">
<item
android:id="@+id/i1"
android:title="asdfa"></item>
<item
android:id="@+id/i2"
android:title="asdfa"></item>
</group>
<item
android:id="@+id/i3"
android:title="asdfa"></item>
<item
android:id="@+id/i4"
android:title="asdfa"></item>
</menu>