There are four types of menus in the Android system: options menu (option menu), context menu (context menu), sub menu (sub menu), and Popup menu (popup menu).
First of all, OptionsMenu
1. Method introduction:
public booleanonCreateOptionsMenu(Menu menu): Use this method to call OptionsMenu.
public booleanonOptionsItemSelected(MenuItem item): The action that occurs after selecting the menu item.
public voidonOptionsMenuClosed(Menu menu): The action that occurs after the menu is closed.
public booleanonPrepareOptionsMenu(Menu menu): The onPrepareOptionsMenu method will be called before the option menu is displayed. You can use this method to adjust the menu according to the situation at that time.
public booleanonMenuOpened(int featureId, Menu menu): The action that occurs after single opening.
2. Default style
The default style is to pop up a menu at the bottom of the screen. We call this menu OptionsMenu. Generally speaking, the option menu displays up to 2 rows of 3 menu items in each row. These menu items have text and icons, also called Icon Menus. If there are more than 6 items, they will be hidden from the sixth item. In the sixth item, a More will appear. Click More to appear the sixth item and the subsequent menu items. These menu items are also called Expanded Menus. The following is introduced.
1. Overload the onCreateOptionsMenu(Menu menu) method
@Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. //getMenuInflater().inflate(, menu); (0,1,1,"set up").setIcon(); (0,3,3,"set up").setIcon(); (0,2,2,"download").setIcon(); }
Overload the onCreateOptionsMenu(Menu menu) method, add menu items to this method, and finally return true, if false, the menu will not be displayed.
Notice:
Most mobile phones will have a "MENU" key on them. After an application is installed on the phone, the menu associated with the application can be displayed through "MENU".
However, starting from Android 3.0, Android no longer requires that MENU cases be provided on mobile devices. Although many mobile phones now provide MENU buttons, some of them are no longer provided. In this case, Android recommends using ActionBar instead of menu. In future blog posts, we will introduce Android's support for ActionBar
The optionsMenu on 4.2 is placed on the actionbar. You must set showAsAction="always" in the xml file to display it on the actionbar. Only the menu displayed on the actionBar will have icons. To set it in the code, (id).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS)
2. Overload the onOptionsItemSelected(MenuItem item) method to capture menu item events
@Override public boolean onOptionsItemSelected(MenuItem item) { // TODO Auto-generated method stub if (() == 0 &&() == 1) { Intent intent = new Intent(this, ); startActivity(intent); } else { (this, (), Toast.LENGTH_SHORT).show(); } return (item); }
ContextMenu
When the user long presses a control, the menu that pops up is called the context menu. The menu that pops up with right-click in Windows is the context menu.
1. Overload the onCreateContextMenu() method of Activity, call Menu's add method to add menu item MenuItem
@Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { (0, 1, 0, "Red Background"); (0, 2, 0, "Green Background"); (1, 3, 0, "White Background"); // TODO Auto-generated method stub (menu, v, menuInfo); }
2. Overload the onContextItemSelected() method and respond to the menu click event
@Override public boolean onContextItemSelected(MenuItem item) { // TODO Auto-generated method stub switch(()) { case 1: (); break; case 2: (); break; case 3: (); break; } return true; }
3. Overload the registerForContextMenu() method to register the context menu for the view
private TextView myText; @Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(.activity_main); myText = (TextView)findViewById(); registerForContextMenu(myText); }
Submenu (SubMenu)
A submenu is a menu that displays the same functions in multiple levels. For example, the Windows "File" menu includes submenu such as "New", "Open", and "Close".
Methods to create submenu
1. Overload the onCreateOptionsMenu() method of Activity, call Menu's addSubMenu() method to add submenu items
2. Call SubMenu's add() fry and add submenu items
@Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. SubMenu subMenu = (0, 4, 4, "Login/Register"); (1, 1, 1, "Log in"); (1, 2, 2, "register"); return true; }
3. Overload the onOptionsItemSelected(MenuItem item) method to capture menu item events
This method has a MenuItem parameter, which can be used to determine which menu item is clicked using its getTitle and getItemId methods
public boolean onOptionsItemSelected(MenuItem item) { // TODO Auto-generated method stub if(().equals("Log in")) { ("action:","Click to register/login"); } return (item); }
SubMenu is a subinterface of Menu. After adding SubMenu, you can add its submenu items through methods. Images cannot be displayed on submenu items, but images can be displayed at the head of the submenu, but submenu items can have check boxes and option buttons.
Note: Submenu cannot be added to submenu;
Popup menu (Popup)
This type of menu needs to be bound to a View. When clicking the View, the menu pops up on or below the View (depending on the screen space).
How to use:
1. Create XML menu resource file;
<menu xmlns:andro > <item android: android:orderInCategory="100" android:showAsAction="never" android:title="edit"/> <item android: android:orderInCategory="100" android:showAsAction="never" android:title="popup"/> </menu>
Steps 2 to 5 can be implemented in the click event that binds the View!
2. Create a PopupMenu object and instantiate the incoming context and bound View;
3. Use the getMenuInflater().inflate() of the PopupMenu object to press the XML menu file into it.
4. Use the setOnMenuItemClickListener of the PopupMenu object itself to set click event;
5. Use the show of the PopupMenu object itself to display the popup menu.
public void showPopuMenu(View v) { PopupMenu popup = new PopupMenu(, v); MenuInflater inflater = (); (, ()); (); }
The above content is the menu menu for Android development introduced to you by the editor. I hope you like it.