As an important part of Google's vision, the Android menu type will further promote the implementation of the enterprise goal of "providing information to everyone anytime, anywhere", which not only meets the needs of users in all aspects, it also puts some pressure on apple.
The options menu is displayed by pressing the home key. The context menu needs to be displayed after pressing the top 2 S button on The view. Both menus can be added to sub-menus. Sub-menus cannot be nested. The options menu can display up to six menu options at the bottom of the screen, which is called the icon menu. The icon menu cannot have the checkable option. More than 6 menu items will be called out with more icon menu.
It is called the expanded menu. Options menu is generated by onCreateOptionsMenu of the activity. This function is called only when the menu is generated for the first time. Any idea of changing the options menu can only be implemented in onPrepareOptionsMenu. This function will be called before the menu is displayed. OnOptionsItemSelected is used to process selected menu items.
Context menu is bound to a specific view. In activity, registerForContextMenu is used to register context menu for a view. Before the context menu is displayed, onCreateContextMenu is called to generate the menu. OnContextItemSelected is used to process selected menu items.
The Android menu type also provides the ability to group menu items. Similar menu items can be divided into the same group, so that you can call setGroupCheckable, setGroupEnabled, setGroupVisible is used to set menu properties, instead of being set separately.
This is a standard method for inserting a menu item. The menu item id is MENU_ITEM_INSERT. The following code is interesting:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content" android:id="@+id/TextView01"/>
-
- <Button android:id="@+id/Button01" android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="@string/txtInfo"></Button>
- </LinearLayout>
What is the purpose of this? In fact, this is a dynamic menu technology (also a bit like plug-in mechanism). If an activity, its type is "android. intent. category. ALTERNATIVE ", the data is" vnd. android. cursor. dir/vnd. google. note.
The system adds a menu item for this activity. Check the Android menu type and find that no activity meets the conditions. Therefore, this code does not dynamically add any menu item. What's going on? After checking the code, I found that it was originally a ghost of onPrepareOptionsMenu! This function runs after onCreateOptionsMenu. In the following code.
Because Menu. CATEGORY_ALTERNATIVE points to the same group, so the Menu items set in onCreateOptionsMenu are overwritten, because onPrepareOptionsMenu does not give the Menu. CATEGORY_ALTERNATIVE has a new value, so Menu. CATEGORY_ALTERNATIVE is null.