Sub-menu
A sub-menu is a menu that displays multiple levels of groups with the same function.
To create a sub-menu:
1. overwrite the onCreateOptionsMenu () method of the Activity, and call the addSubMenu () method of the Menu to add sub-menus.
2. Call the add () method of SubMenu to add sub-menus.
3. overwrite the onContextItemSelected () method to respond to the Click Event of the sub-menu.
As for the onCreateOptionsMenu () method, I have already introduced it before. Now let's look at the addSubMenu () method of Menu. Like the add () method, it has four overload methods and the parameters are the same. Here we will not explain much more.
Public abstract SubMenu addSubMenu (int groupId, int itemId, int order, CharSequence title)
Public abstract SubMenu addSubMenu (int groupId, int itemId, int order, int titleRes)
Public abstract SubMenu addSubMenu (CharSequence title)
Public abstract SubMenu addSubMenu (int titleRes)
The returned objects of these four functions are submenus. Let's take a look at SubMenu and learn about its attributes and methods at will:
SubMenu is used to implement Menu
Commonly used methods include setHeaderIcon () and setHeaderTitle ()
The following two instances are used to familiarize themselves with SubMenu.
Instance 1:
Package com. jiahui. activity;
Import java. lang. reflect. Field;
Import android. app. Activity;
Import android. OS. Bundle;
Import android. view. Menu;
Import android. view. MenuItem;
Import android. view. SubMenu;
Public class SubMenuDemoActivity extends Activity {
Private static final int ITEM1 = Menu. FIRST;
Private static final int ITEM2 = Menu. FIRST + 1;
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
}
Public boolean onCreateOptionsMenu (Menu menu ){
SubMenu file = menu. addSubMenu ("file ");
SubMenu edit = menu. addSubMenu ("edit ");
File. add (0, ITEM1, 0, "new ");
File. add (0, ITEM2, 0, "open ");
Return true;
}
Public boolean onOptionsItemSelected (MenuItem item ){
Switch (item. getItemId ()){
Case ITEM1:
SetTitle ("new file ");
Break;
Case ITEM2:
SetTitle ("Open File ");
Break;
}
Return true;
}
}
Effect:
Instance 2:
Package com. jiahui. activity;
Import android. app. Activity;
Import android. OS. Bundle;
Import android. view. Menu;
Import android. view. SubMenu;
Public class SubMenuDemo_2Activity extends Activity {
Private static final int UPLOAD = Menu. FIRST;
Private static final int DOWNLOAD = Menu. FIRST + 1;
Private static final int SUB_UPLOAD_A = Menu. FIRST + 2;
Private static final int SUB_UPLOAD_ B = Menu. FIRST + 3;
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
}
@ Override
Public boolean onCreateOptionsMenu (Menu menu ){
SubMenu uploadSubMenu = menu. addSubMenu (0, UPLOAD, 0, "UPLOAD ");
UploadSubMenu. setIcon (R. drawable. upload );
UploadSubMenu. setHeaderIcon (R. drawable. upload); // you can specify the sub-menu title.
UploadSubMenu. setHeaderTitle ("Upload parameter settings"); // you can specify that the sub-menu title is "Upload" by default"
UploadSubMenu. add (0, SUB_UPLOAD_A, 0, "Upload parameter 1 ");
UploadSubMenu. add (0, SUB_UPLOAD_ B, 0, "Upload parameter 2 ");
SubMenu downloadSubMenu = menu. addSubMenu (0, DOWNLOAD, 0, "DOWNLOAD ");
DownloadSubMenu. setIcon (R. drawable. download );
Return super. onCreateOptionsMenu (menu );
}
}
Effect:
From: jiahui524 Column