Let’s Talk Android (Chapter 254: Toolbar Navigation 3 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!

Look at the officials, we introduced how to display the navigation in the Toolbar in the last time. In this chapter, we will introduce how to add response events to the navigation. Because the navigation light has an icon is not enough, we need to add content to let the navigation know Specific orientation.

There are two ways to add response events to navigation:

  • One is to configure the parent attribute in the manifest file. The specific code is as follows:
   <activity
            android:name=".ActivityB"
            android:parentActivityName=".ActivityA">
   </activity>
  • The other is to set up a listener, similar to a Button listener. The specific code is as follows:
 mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
    
    
        @Override
        public void onClick(View v) {
    
    
            Toast.makeText(NaviActivity.this,"back clicked",Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(NaviActivity.this,ActivityA.class);
            startActivity(intent);
        }
    });

I personally think that the listener is more convenient and can do many things. Unlike the parrent attribute, it can only navigate to the specified Activity, and it has limitations: it only works when the Actionbar is not removed. If the Actionbar is removed, There is no navigation effect, no matter how you press it, there will be no response.

After adding the navigation response event, we will introduce how to modify the icon displayed in the navigation. The default icon is a white arrow. We can modify the Toolbar property to replace it. The specific code is as follows:

   app:navigationIcon="@mipmap/ic_download"

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/107028851