Http://hi.baidu.com/xmjmcai/blog/item/f22a468b1400ae8fa4c272a9.html
Http://hi.baidu.com/xmjmcai/blog/item/f22a468b1400ae8fa4c272a9.html
1. Menu type
1) optionsmenu selection menu
This is the most common menu for applications. The menu that appears when you press the menu button.
2) contextmenu context menu
A menu popped up when a view is long pressed. Theoretically, it is the same as right-click under XP.
3) submenu
Multi-level menus can be used with the other two menus.
2. How to Use the menu?
1) menus are usually implemented through xml configuration, and few codes are used to implement menus. The advantage of using XML is that you can easily see the structure of the menu. XML needs to be saved in the Res/menu/directory so that it can be compiled into a menu class by Android.
Android: Id attribute: used for recognition when a menu item is selected.
2) 1) the menu defined in does not distinguish it from optionsmenu or contextmenu. The following is the code for filling the menu.
3. Create optionsmenu
1) optionsmenu is usually the most basic action of activity. In android2.3 and earlier, optionsmenu is displayed in action
Bar ,.
2) by default, optionsmenu has been implemented for the activity, but MEMU is empty. In oncreateoptionsmenu (menu
Menu) Fill in the menu in the callback function.
3) when the user clicks the menu, the activity will return menuitem, get its ID through getitemid, and perform subsequent operations based on the returned ID against the Android: Id attribute of XML. Directly paste the Code:
It is worth noting that the return of ture indicates that it has been processed, and the return of false indicates that it has not been processed. The system will pass the menuitem to the parent class for processing.
4) Step 3), android3.0 provides another processing method. That is, the Android: onclick attribute is defined in the menuitem in XML, and its value corresponds to the member method name in the activity. In this case, this member method is used to process menuitem clicks.
5) if the optionsmenu of multiple activities in your application is consistent. To provide reusability, oncreateoptionsmenu and onoptionsitemselected of the activity are implemented, and then the activity inherits from the activity.
Of course, the above does not need to be completely consistent. You only need to define the common menuitem of all activities in the parent class. The subclass can add a new menuitem through menu. Add. Because each activity maintains its own menu, it does not affect the menu of other activities.
6) The activity creates a menu in oncreateoptionsmenu. This function is called only once. Android2.3 and previously called when optionsmenu was used for the first time. Android3.0 and later are called when you create an activity. This means that you cannot modify the menu.
To be able to modify the menu, the android system provides the onprepareoptionsmenu function, in which you can delete, add, and enable menuitem. Android2.3 and the onprepareoptionsmenu will be called each time optionsmenu is displayed. Android3.0 and later, this function will be called only after invalidateoptionsmenu is called.
Because touch screens do not have focus. That is, when you use the touch screen to select a menuitem, The menuitem does not have focus. Therefore, do not use the Focus feature to modify the menu. If you need to use the Focus feature, you can select contextmenu.
4. Create a contextmenu.
1) The right-click function is similar to that of XP. It is a view-based menu. Because it is based on view, it can solve the problem that the touch screen has no focus. Contextmenu applies to all views, but usually listview is used in applications. To prompt the user to have a contextmenu, listview usually uses background changes as a prompt.
2) To use contextmenu, you must first specify the view to use. Call registerforcontextmenu (View) to notify the activity.
3) Implement oncreatecontextmenu and oncontextitemselected with optionsmenu
It is worth noting that a single activity can have multiple views to register the contextmenu menu. How can we implement different contextmenus? This is also the role of the view parameter in oncreatecontextmenu.
Adaptercontextmenuinfo info = (adaptercontextmenuinfo) item. getmenuinfo (); is used to determine which item of listview is right-clicked.
4) How to dynamically modify the contextmenu? Not introduced on the official website.
5. Create a submenu
1) nested <menu> in XML. To be verified: Android menus worth my attention seem to only support level-2 menus, that is, only two layers can be nested.
2) When the menuitem In the submenu is clicked, there is no special processing. Because the system regards the submenu as a part of the menu, the processing method is the same as that of its menu, that is, optionsmenu and contextmenu.