Use of Menu level 1 Menu and level 2 Menu of Android learning notes, and android learning notes
(1) No changes have been made to the layout file.
(2) the code in the main. xml file under the res -- menu directory is as follows:
<Menu xmlns: android = "http://schemas.android.com/apk/res/android"> <! -- Level 1 menu --> <item android: id = "@ + id/file" android: title = "@ string/file"> <! -- Level 2 menu --> <menu> <item android: id = "@ + id/create" android: title = "@ string/create"> </item> <item android: id = "@ + id/open" android: title = "@ string/open"> </item> </menu> </item> <! -- Level 1 menu --> <item android: id = "@ + id/chioce" android: title = "@ string/chioce"> <! -- Level 2 menu --> <menu> <item android: id = "@ + id/create1" android: title = "@ string/create"> </item> <item android: id = "@ + id/open1" android: title = "@ string/open"> </item> </menu>
(3) In the string. xml file under the values directory:
<? Xml version = "1.0" encoding = "UTF-8"?> <Resources> <string name = "app_name"> Menu_submenu </string> <string name = "action_settings"> Settings </string> <string name = "hello_world"> Hello world! </String> <string name = "file"> file </string> <string name = "create"> New </string> <string name = "open"> open </string> <string name = "chioce"> options </string> </resources>
(3) The MainActivity. java code is as follows:
Package com. example. menu_submenu; import android. OS. bundle; import android. app. activity; import android. content. intent; import android. view. menu; import android. view. menuItem; import android. view. subMenu; import android. widget. toast; public class MainActivity extends Activity {@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main) ;}@ Overridepublic boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. main, menu); return true;}/** processing level-1 menu */@ Overridepublic boolean onOptionsItemSelected (MenuItem item) {switch (item. getItemId () {case R. id. file: Toast. makeText (MainActivity. this, "Level 1 menu file .. ", Toast. LENGTH_SHORT ). show (); break; case R. id. chioce: Toast. makeText (MainActivity. this, "Level 1 menu chioce .. ", Toast. LENGTH_SHORT ). show (); break; default: break;} return super. onOptionsItemSelected (item);}/** process level-2 menu */@ Overridepublic boolean onMenuItemSelected (int featureId, MenuItem item) {switch (item. getItemId () {case R. id. create1: Toast. makeText (MainActivity. this, "Process Level 2 menu create1 .. ", Toast. LENGTH_SHORT ). show (); break; case R. id. open1: Toast. makeText (MainActivity. this, "Process Level 2 menu create1 .. ", Toast. LENGTH_SHORT ). show (); break; case R. id. create: Toast. makeText (MainActivity. this, "Process Level 2 menu create .. ", Toast. LENGTH_SHORT ). show (); break; case R. id. open: Toast. makeText (MainActivity. this, "Process Level 2 menu open .. ", Toast. LENGTH_SHORT ). show (); break; default: break;} return super. onMenuItemSelected (featureId, item );}}