Defining the Options Menu Using XML

What is the options menu?

The Options menu is an Activity's main menu item where you place actions that affect your app globally, such as Search, Compose Email, and Settings. The placement of items in the options menu on the screen depends on the version of Android you are developing your app on:

  • If you develop an app for Android 2.3.x (API level 10) or lower, the content of the options menu will appear at the bottom of the screen as shown below, and when there are more than 6 menu items, the sixth item will be displayed and the rest of the items into the overflow menu (the More menu item in the picture).

  • If you develop an app for Android 3.0 (API level 11) and higher, the content of the options menu will appear in the app bar as shown below. By default, the system will put all menu items into the action overflow menu (that is, the menu item labeled 3 in the figure).

Create menu resource file

First I create a my_menu.xml file in the app/src/mian/res/menu/ directory

The content is as follows:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/item1"
        android:icon="@mipmap/ic_launcher"
        android:title="item1"/>

    <item
        android:id="@+id/item2"
        android:icon="@mipmap/ic_launcher"
        android:title="item2"/>

    <group
        android:id="@+id/group"
        android:checkableBehavior="single">
        <item
            android:id="@+id/group_item1"
            android:icon="@mipmap/ic_launcher"
            android:title="group_item1"/>

        <item
            android:id="@+id/group_item2"
            android:icon="@mipmap/ic_launcher"
            android:title="group_item2"/>
    </group>

    <item
        android:id="@+id/submenu"
        android:title="submenu_title">
        <menu>
            <item
                android:id="@+id/submenu_item1"
                android:title="submenu_item1"/>
        </menu>
    </item>

</menu>

We can see from the code <menu>that root node and uses <item>elements to define menu items. At the same time, we can also choose to use <group>elements to define a group to uniformly modify some characteristics of the menu items in the group, such as whether they are visible or not. Finally, you can see that we create submenus <item>by nesting elements within elements.<menu>

<item>Common attributes of elements:

  • android:id specifies a unique identifier for the menu item
  • android:title specifies the title of the menu item
  • android:icon specifies the icon of the menu item
  • android:showAsAction Specifies when and how menu items are displayed in the app bar as action items

<group>Common Attributes of Elements

  • android:id specifies a unique identifier for the shuffle menu
  • android:checkableBehavior specifies the selection behavior of the shuffled menu
  • android:visible specifies whether the shuffle menu is visible
  • android:enable specifies whether the shuffle menu is available

For a more detailed introduction, please refer to the Menu section of the API documentation .

Load menu resource file

Specify the options menu for the Activity by overriding the onCreateOptionsMenu() method.

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.my_menu, menu);
        return true;
    }
}

running result:

Handling click events

When we select an item from the options menu, the system calls the Activity's onOptionsItemSelected() method. This method is passed the selected MenuItem, which can be identified by calling the getItemId() method, which returns the menu item's unique ID (defined by the android:id attribute in the menu resource). We can match this ID with a known menu item to perform the appropriate action. E.g:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.my_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.group_item1:
                item.setChecked(true);
                return true;
            case R.id.group_item2:
                item.setChecked(true);
                return true;
            default:
                Toast.makeText(this, "selected " + item.getTitle(), Toast.LENGTH_SHORT).show();
                return super.onOptionsItemSelected(item);
        }
    }
}

running result:

Summarize

This article mainly introduces how to use XML to define the option menu, and the methods for defining other types of menus are similar. For more in-depth understanding and use of the menu, please refer to the official Android tutorial . For the problem that items are still displayed in the overflow even with showAsAction="always" set, please refer to this article .

ps: Welcome to leave a message to communicate and make progress together.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325819021&siteId=291194637