3.App Resources-Resource Types/Menu Resource
1. Menu Resource
A menu resource defines an application menu (Options Menu, Context Menu, or submenu) that can be inflated with MenuInflater
.
//res/menu/example_menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/item1" android:title="item1" android:showAsAction="ifRoom|withText"/> <item android:id="@+id/submenu" android:title="submenu_title" android:showAsAction="ifRoom|withText" > <menu> <item android:id="@+id/submenu_item1" android:title="submenu_item1" /> <item android:id= "@+id/submenu_item2" android:title="submenu_item2" /> </menu> </item> <group android:id="@+id/group"> <item android:id="@+id/group_item1" android:title="group_item1" /> <item android:id="@+id/group_item2" android:title="group_item2" /> </group> </menu>
public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.example_menu, menu); return true; }
Pay attention to the property "showAsAction"
When and how this item should appear as an action item in the Action Bar.
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步