Android implements the right-click pop-up menu effect similar to that in PC

The ContextMenu in Android system is similar to the right-click pop-up menu in PC. when a view is registered to a context menu, execute a "long press" action on the object, and a floating menu providing relevant functions will appear. The context menu can be registered in any view object, but the most common is the item used in the list view listview. When you press the list item, its background color will be changed, and the prompt will render the context menu. Note: context menus do not support icons and shortcut keys. In order to create a context menu, you must override the context menu callback functions of the activity: oncreatecontextmenu() and oncontextitemselected(). In the callback function oncreatecontextmenu (), you can add menu items by using an add () method, or by expanding a menu resource defined in XML. Then, register a context menu ContextMenu for this view through registerforcontextmenu(). Next, I will show the basic use of ContextMenu through an example demo, and configure menu options by manually adding menu menu menu and XML file. 1. The engineering structure catalog of the demo is as follows:

2. The source code of the main.xml layout file in the RES / layout directory is as follows:

3. The source code of cmenu.xml menu option file in RES / menu directory is as follows:

4. The source code of menuactivity.java under com.andyidea.menudemo package is as follows:

5. The screenshot of demo after running is as follows:

I hope this article will help you learn Android Software Programming.

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