Android Studio -- create Menu items, androidmenu
In most android apps, a menu button is set in the upper right corner of the interface, for example, the plus sign in the upper right corner.
In this case, you need to create a folder named menu in the directory of the same level as layout, and right-click the newly created menu to create an xml file:
Xml file code:
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <item android:id="@+id/id_action_add" app:showAsAction="always" android:title="add" android:icon="@mipmap/ic_launcher" android:orderInCategory="1" /> <item android:id="@+id/id_action_delete" app:showAsAction="always" android:title="jian" android:icon="@mipmap/ic_launcher" android:orderInCategory="1" /></menu>
The two items above represent the buttons on your menu bar. Android: Images of menu items that can be customized in the icon
The following is the MainActivity code to be rewritten:
@ Override public boolean onCreateOptionsMenu (Menu menu) {// the newly created xml file getMenuInflater (). inflate (R. menu. main, menu); return super. onCreateOptionsMenu (menu) ;}@ Override public boolean onOptionsItemSelected (MenuItem item) {// click a button Based on different IDs to control the event switch (item. getItemId () {case R. id. id_action_add: // event break; case R. id. id_action_delete: // event break;} return true ;}
Memo .....