Android menu Development
Menu category:
OptionsMenu context menu SubMenu)
Pop-up menu (
Popup)
First, the option menu (OptionsMenu)
I. Method Introduction:
Public booleanonCreateOptionsMenu (Menu menu): use this method to call OptionsMenu.
Public booleanonOptionsItemSelected (MenuItem item): The action that occurs after the menu item is selected.
Public voidonOptionsMenuClosed (Menu menu): The action that occurs after the Menu is closed.
Public booleanonPrepareOptionsMenu (Menu menu): The onPrepareOptionsMenu method is called before the option Menu is displayed. You can use this method to adjust the menu according to the current situation.
Public booleanonMenuOpened (int featureId, Menu menu): action that occurs after a single open.
Ii. Default Style
The default style is to bring up a menu at the bottom of the screen. This menu is called the option menu OptionsMenu. In general, the option Menu displays a maximum of two menu items in each row, these menu items have text icons, also known as Icon Menus. If there are More than 6 items, they will be hidden from the sixth item, and a More item will appear in the sixth item, click More to display the Sixth and subsequent menu items. These menu items are also called Expanded Menus. The following section describes how to create an ECS instance.
1. Reload the onCreateOptionsMenu (Menu menu) Method
@ 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); menu. add (0, 1, 1, set ). setIcon (R. drawable. setting); menu. add (0, 3, 3, set ). setIcon (R. drawable. setting); menu. add (0, 2, download ). setIcon (R. drawable. download );}
Reload the onCreateOptionsMenu (Menu menu) method, add a Menu item to this method, and return true. If it is false, the menu is not displayed.
Note:
Most mobile phones have a "MENU" key. After an application is installed on a mobile phone, you can use "MENU" to display the menus associated with the application. However, since Android 3.0, Android does not require that menus be provided on mobile devices. Although many mobile phones still 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.
OptionsMenu on 4.2 is placed on the actionbar. You must set showAsAction = always living showAsAction = ifRoom in the xml file to display it on the actionbar. Icons are available only for menus displayed on the actionBar. To be set in the code, menu. findItem (id). setShowAsAction (MenuItem. SHOW_AS_ACTION_ALWAYS)
2. Reload the onOptionsItemSelected (MenuItem item) method to capture menu item events
@Overridepublic boolean onOptionsItemSelected(MenuItem item){// TODO Auto-generated method stubif (item.getGroupId() == 0 &&item.getItemId() == 1){Intent intent = new Intent(this, SecondActivity.class);startActivity(intent);}else{Toast.makeText(this, item.getTitle(), Toast.LENGTH_SHORT).show();}return super.onOptionsItemSelected(item);}
Context Menu)
When you press a widget for a long time, the pop-up menu is called context menu. In Windows, right-click the context menu.
1. Reload the onCreateContextMenu () method of the Activity and call the add method of Menu to add the Menu item MenuItem.
@ Overridepublic void onCreateContextMenu (ContextMenu menu, View v, ContextMenuInfo menuInfo) {menu. add (0, 1, 0, red background); menu. add (0, 2, 0, green background); menu. add (1, 3, 0, white background); // TODO Auto-generated method stubsuper. onCreateContextMenu (menu, v, menuInfo );}
2. Reload the onContextItemSelected () method and click the event in the response menu.
@Overridepublic boolean onContextItemSelected(MenuItem item){// TODO Auto-generated method stub switch(item.getItemId()) { case 1: myText.setBackgroundColor(Color.RED); break; case 2: myText.setBackgroundColor(Color.GREEN); break; case 3: myText.setBackgroundColor(Color.WHITE); break; } return true;}
3. Reload the registerForContextMenu () method to register the context menu for the view
private TextView myText;@Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);myText = (TextView)findViewById(R.id.mytext);registerForContextMenu(myText); }SubMenu)
A sub-menu is a type of menu that displays multiple levels for groups with the same function. For example, "new" and "open" appear in the "file" menu of Windows ", "Close" and other sub-menus.
How to create a sub-menu
1. Reload the onCreateOptionsMenu () method of the Activity and call the addSubMenu () method of the Menu to add sub-Menu items.
2. Call add () of SubMenu to add sub menu items.
@ Overridepublic boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. subMenu subMenu = menu. addSubMenu (0, 4, 4, Logon/registration); subMenu. add (1, 1, 1, login); subMenu. add (1, 2, 2, registration); return true ;}
3. Reload the onOptionsItemSelected (MenuItem item) method to capture menu item events
This method has a MenuItem parameter. You can use its getTitle and getItemId methods to determine which menu item is clicked.
@ Overridepublic boolean onOptionsItemSelected (MenuItem item) {// TODO Auto-generated method stubif (item. getTitle (). equals (LOGIN) {Log. e (action:, click Register/log on);} return super. onOptionsItemSelected (item );}
SubMenu is a sub-interface of Menu. After adding SubMenu, you can use SubMenu. add to add its sub-Menu items. The image cannot be displayed in the submenu item, but the image can be displayed in the submenu header. However, the submenu item can contain check boxes and option buttons.
Note: submenus cannot be added;
Pop-up menu (
Popup)
This type of menu needs to be bound to a View. When you click this View, the menu is displayed above or below the View (depending on the screen space. Usage: 1. Create an XML menu resource file;
Popup. xml
2 ~ Step 5 can be implemented in the Click Event of the bound View! 2. Create a PopupMenu object, instantiate the input context and bound View; 3. Use getMenuInflater () of the PopupMenu object (). inflate (): press the XML menu file. 4. Use setOnMenuItemClickListener of the PopupMenu object to set the click event. 5. Use show to display the pop-up menu of the PopupMenu object.
public void showPopuMenu(View v){PopupMenu popup = new PopupMenu(MainActivity.this, v); MenuInflater inflater = popup.getMenuInflater(); inflater.inflate(R.menu.popup, popup.getMenu()); popup.show();}