Definition of Android menu and implementation of actionbar

The option menu of Android is the main menu item of an activity:

There are two ways to create it

The first method:

When the user clicks the menu key, the oncreateoptionmenu () method will be triggered, so if you need to have your own implementation, you must override this method. This method will pass in a menu object. You can add a submenu by using the add () method of this object. Similarly, submenus can add menu items for themselves through the add () method.

Of the four parameters of the add() method:

The first parameter is groupid, which can be used to control whether each menu item is in the same group.

The second parameter is itemid, which is the ID of each menu item.

The third parameter is order, which controls the order of each item. If you don't care about order, you can pass in none or 0. The smaller the value of the corresponding int type, the higher the order of menu items. For example, the menu item with order = 1 is higher than that with order = 2.

The fourth parameter is title, which corresponds to the title of the menu item.

Using java code to implement various menu items may be more suitable for thinking habits, but it often leads to bloated code.

The following describes how to define an option menu in XML:

To define a menu in an XML file, first create a folder named menu in the resource file;

Then create an XML file in it:

The root node of the XML file must be menu;

Each item tag defines a menu item, and sub menus can be created by redefining < Menu > in < item >.

Each item supports defining many attributes, the most important of which are ID, icon, title and showasaction;

The first three items are easy to understand, mainly because showasaction is difficult to understand. The value of showasaction has five alternatives and supports combination:

Here we need to clarify a concept:

As shown in the above figure, the one with a title and two buttons is called actionbar. It has been introduced since Android 3.0. The system uses actionbar as the toolbar by default. Items in the actionbar can be displayed in two ways -- buttons and overflow menus.

In other words, Android's menu is no longer a simple menu. It can be said that it is a part of actionbar. It can also be said that the menu items can be displayed as buttons in the actionbar (by setting the value of showasaction).

As shown in the figure below, two icons represent buttons and three vertical points represent overflow menus:

To get back to business, about the value of showasaction:

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>