menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android" >
<group android:id="@+id/group1">
<item android:id="@+id/item1" android:title="해태"></item>
<item android:id="@+id/item2" android:title="쌍방울"></item>
<item android:id="@+id/item3" android:title="삼미"></item>
<item android:id="@+id/item4" android:title="다른팀">
<menu>
<item android:id="@+id/item5" android:title="빙그레"/>
<item android:id="@+id/item6" android:title="기아"/>
</menu>
</item>
</group>
</menu>
MenuActivity.java
package com.gusfree.menu;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
public class MenuActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
//최초로 menu버튼 누를때 호출됩니다.
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// 뷰를 불러올때 View.inflate(this, R.layout.main, null);
//menu폴더안에 있는 menu.xml을 불러와서 사용하자
MenuInflater menuInflate=new MenuInflater(this);
menuInflate.inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Toast.makeText(this,
item.getTitle()+", id:"+item.getItemId(),0).show();
switch(item.getItemId()){
case R.id.item1:
break;
case R.id.item2:
break;
}
return super.onOptionsItemSelected(item);
}
}