Learn Android menu together

Menu is a common user interface component in Android development. By using menu API, users can provide common and consistent experience. This article mainly explains the relevant contents of the three menus.

Set the option menu by overriding the oncreateoptionsmenu method in the activity; Handle the event function of the options menu by overriding the optionsitemselected method.

As shown in the following figure: the option menu is displayed by three white dots in the upper left corner of the app. When clicked, the menu content pops up.

The key codes are as follows:

Set the context menu by overriding the oncreatecontextmenu method in the activity, and handle the events of the context menu by overriding the oncontextitemselected method.

As shown in the figure below, you can pop up the context menu by long pressing the control.

The key codes are as follows:

The context menu needs to be registered with the specified control

Declare the pop-up menu by PopupMenu PopupMenu = new PopupMenu (this, V). The second parameter is the control on which the menu is anchored, that is, on which control the menu is docked.

As shown in the figure below: the menu pops up when you click the button

The key code is as follows. Call the show () method to display the menu:

There are many functional application scenarios of menu items. This paper aims to attract jade and learn together.

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
分享
二维码
< <上一篇
下一篇>>