Android toolbar control details and examples

Toolbar control details

Add toolbar in activity

1. Add library

2. To inherit appcompatactivity

3. Set theme

Use the toolbar to hide the default action bar of the system

4. Add toolbar layout in XML

The material design specification recommends setting the elevation to 4dp

The toolbar is placed at the top of the activity (equal to nonsense)

5. Set toolbar

In the onCreate () method of Activity, we call the setSupportActionBar () method to pass in our ToolBar object, and set the ToolBar added in our XML to App Bar of our Activity page.

Now, there should be an app bar in our page. By default, this app bar will only display the name of an app and a button with drop-down options. You can also add more option buttons to the app bar

Add and process an action

The toolbar allows you to add action buttons on it. However, because the space on the toolbar is limited, if a program has too many actions, you can add actions to the drop-down menu instead of displaying them on the toolbar

Add an action button

You can define all the action buttons we want to add and the actions in the drop-down list in the XML file. If you want to add actions, you can create a new XML file in the RES / directory and add elements. For example

The app: showasaction property is used to set where the action is displayed. If we set app: showasaction = "ifroom" (the most commonly used method in the example), this action will be displayed on the toolbar. If there are not enough controls on the toolbar, it will be displayed in the drop-down menu. If it is set to app: showasaction = "never" in the program, the action will always be displayed in the drop-down list rather than on the toolbar. Response operation (callback)

When the user selects a Action, the system will callback the onOptionsItemSelected () method and pass the MenuItem object. In the implementation of onOptionsItemSelected (), the MenuItem.getItemId () method is called to determine which item is pressed. The returned ID matches the value you declared in the Android: ID attribute of the corresponding element.

For example, below

Add a button to return to the main interface

In order to make it easy for users to return to the main interface, the toolbar can add a button to directly return to the specified main interface.

Declare main interface (parent interface)

It needs to be declared in the manifest file by setting the Android: parentactivityname attribute. If you want to support older versions of Android devices, it needs to be defined, like this:

Using the back button

To use the returned button, you need to call the setdisplayhomasupenabled() method, like this:

Add action views

It is similar to adding and processing an action above, except that you can set showasaction to "ifroom|collapseactionview" or "never|collapseactionview", which is similar to this

Here we add a search button with search function. If we want to customize a button of our own, we don't need to add actionviewclass

If you want to configure this action, you can get the search object through getactionview () in the callback of oncreateoptionsmenu(), like this:

Thank you for reading, hope to help you, thank you for your support to this site!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>