to create Menu @ JAVA & XML ....
first, to tell you how to do. just to fill the following Method provided by Activity.
public boolean onCreateOptionsMenu(Menu menu)
public boolean onOptionsItemSelected(MenuItem item)
public boolean onPrepareOptionsMenu(Menu menu)
3 method above is simple to understand.
to create Menu both @JAVA and @XML is to fill 3 method above.
The following is to do @ JAVA
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, Menu1, 0, "Menu-1").setIcon(R.drawable.se32);
menu.add(0, Menu2, 0, "Menu-2").setIcon(R.drawable.heart);
menu.add(0, Menu3, 0, "Menu-3");
menu.add(0, Menu4, 0, "Menu-4");
menu.add(0, Menu5, 0, "Menu-5");
menu.add(0, Menu6, 0, "Menu-6");
menu.add(0, Menu7, 0, "Menu-7");
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case Menu1:
//to fill action listened by MenuItem
break;
}
return false;
}
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem item = menu.findItem(Menu1);
item.setEnabled(false);
//to query menu
//MenuItem item = menu.findItem(MENU_QUIT);
//item.setChecked(false);
//item.setTitle("update");
/* to update menu
if(FLAG_STATUS == 0){
item.setTitle("QUIT-1");
FLAG_STATUS = 1;
}
else {
item.setTitle("QUIT-0");
FLAG_STATUS = 0;
} */
/* to add menu
if(FLAG_STATUS == 0){
menu.add(0, MENU_NEW_NEW, 0, "New NEW");
}
*/
return true;
}
aLso, to use Menu @ XML is OK.
1. to create menu by XML file, and name it as "menu_item.xml"
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/Item_1"
android:title="Item_1"
1.3. ·´Éä»úÖÆµÄ¹¦ÄÜ
JAVA·´Éä»úÖÆÖ÷ÒªÌṩÁËÒÔϹ¦ÄÜ£º
1.ÔÚÔËÐÐʱÅжÏÈÎÒâÒ»¸ö¶ÔÏóËùÊôµÄÀà
2.ÔÚÔËÐÐʱ¹¹ÔìÈÎÒâÒ»¸öÀàµÄ¶ÔÏó
3.ÔÚÔËÐÐʱÅжÏÈÎÒâÒ»¸öÀàËù¾ßÓеijÉÔ±±äÁ¿ºÍ·½·¨£¨Í¨¹ý·´ÉäÉõÖÁ¿ÉÒÔµ÷ÓÃprivate·½ ......