Let’s Talk Android (Chapter 258: Toolbar Menu 1 in Android)

Hello everyone, last time we talked about the example of Toolbar in Android, this time we will continue to talk about the example. Stop talking about gossip, and get back to business. Let's talk to Android together!

You guys, we introduced the title of the Toolbar in the last time, and in this chapter we will introduce the menu of the Toolbar. The menu is located on the far right side of the Toolbar. There are three dot icons. After clicking, the menu will pop up. The accurate wording of the menu should be Action, which is usually displayed on the right side of the title. If there are more than one, it will be folded and displayed in the menu. This is controlled by the attribute value, which we will introduce in detail later. The following are the steps to use the menu, please refer to:

  • 1. Create menu directories and files in resources, mainly to add sub-options in the menu. The method of adding is similar to using controls. The labels that are drawn are Menu and Item. They have many attributes, the commonly used ones are title, icon, showAsAction. The following is a code example, we have added six menu sub-options to the example:
 //此文件 位于menu目录下
 <?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item
        android:id="@+id/toolbar_title"
        android:title="Toobar Title"
        app:showAsAction="never" />
    <item
        android:id="@+id/toolbar_tv1"
        android:title="Toobar Item1"
        app:showAsAction="withText" />
    <item
        android:id="@+id/toolbar_tv2"
        android:title="Toobar Item2"
        app:showAsAction="withText" />
    <item
        android:id="@+id/toolbar_tv3"
        android:title="Toobar Item3"
        app:showAsAction="always" />
    <item
        android:id="@+id/toolbar_tv4"
        android:title="Toobar Item4"
        android:icon="@mipmap/ic_download"
        app:showAsAction="always" />
    <!--<item-->
        <!--android:id="@+id/toolbar_tv5"-->
        <!--android:title="Toobar Item5"-->
        <!--app:showAsAction="always" />-->
    <item
        android:id="@+id/download"
        android:title="Download"
        app:showAsAction="ifRoom" />

</menu>
  • 2. Create a menu in the code (implemented by rewriting the callback method):
 public boolean onCreateOptionsMenu(Menu menu) {
    
    
   //这里的toolbar就是步骤1中创建的文件
    getMenuInflater().inflate(R.menu.toolbar,menu);
    return super.onCreateOptionsMenu(menu);
}
  • 3. Add a listener to the menu, which is mainly used to handle the response of the menu items.
@Override
    public boolean onOptionsItemSelected(MenuItem item) {
    
    
        switch (item.getItemId()) {
    
    
            case R.id.toolbar_title:
                Log.i(TAG, "onOptionsItemSelected: menu title ");
                break;
            case R.id.toolbar_tv1:
                Log.i(TAG, "onOptionsItemSelected:  item1");
                break;
            case R.id.toolbar_tv2:
                Log.i(TAG, "onOptionsItemSelected:  item2");
                break;
            case R.id.toolbar_tv3:
                Log.i(TAG, "onOptionsItemSelected:  item3");
                break;
            case R.id.toolbar_tv4:
                Log.i(TAG, "onOptionsItemSelected:  item4");
                break;
            case R.id.download:
                Log.i(TAG, "onOptionsItemSelected:  download");
                break;
            default:
                Log.i(TAG, "onOptionsItemSelected: default: "+item.getItemId());
                break;
        }
        return true;
    }

Look at the officials, we didn't do specific operations in the code, just print a line of log. You can add corresponding functions based on the content of the menu.

Everyone, let’s stop here for the example of Toolbar in Android. If you want to know what other examples are, let’s listen to the next breakdown!

Guess you like

Origin blog.csdn.net/talk_8/article/details/107136835