Use of the toolbar "Android-v"

Source: Internet
Author: User

Toolbar is a control in the Android V7 package that replaces the action Bar as the head title bar layout for the interface. The toolbar is more flexible than the action bar and can be displayed in any position.

First look at the toolbar in the layout file code how to write, in fact, as with other Android native control, only need to set the width of the high, background and other properties can be, if there are other requirements can also set the theme properties such as theme.

Here is a code for toolbar in the layout file:

<Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:app= "Http://schemas.android.com/apk/res-auto"Android:id= "@+id/activity_main"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:background= "#FFFFFFFF">    <Android.support.v7.widget.ToolbarAndroid:id= "@+id/toolbar"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:background= "@color/colorprimary"Android:minheight= "? Attr/actionbarsize"App:popuptheme= "@style/themeoverlay.appcompat.light"App:theme= "@style/themeoverlay.appcompat.actionbar" /></Relativelayout>

After writing toolbar in the layout file, toolbar just shows up as a strip with a background and nothing on it because we don't bind toolbar and activity. We can bind toolbar and activity by invoking the Setsupportactionbar () method in the activity. The code is as follows:

        // make toolbar replace the original Actionbar        Setsupportactionbar (toolbar);

Toolbar can put a lot of controls, see the following picture, at a glance.

Here are some of the code that sets properties for toolbar:

        // to set individual controls in toolbar         Toolbar.setlogo (r.mipmap.ic_launcher);        Toolbar.settitle ("toolbar title");        Toolbar.settitletextcolor (color.red);        Toolbar.setsubtitle ("toolbar Subtitle");        Toolbar.setsubtitletextcolor (color.yellow);        Toolbar.setnavigationicon (Android. R.drawable.ic_menu_camera);

You can draw a different control into toolbar by setting it with the hint of red word. Worth talking about is the rightmost three icons, these three icons are actually from a menu file, that is, the three buttons belong to the same menu, set the Showasaction property to ifroom the item can be like the first two on the way, if there is enough space can be displayed While the item with the Showasacion property set to Never is stored in the rightmost item, clicking on it pops up a drop-down list to show the rest. The contents of the menu file are as follows:

<Menuxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:app= "Http://schemas.android.com/apk/res-auto">    <ItemAndroid:id= "@+id/action_edit"Android:icon= "@android:d rawable/ic_menu_edit"android:orderincategory= "a"Android:title= "Edit"app:showasaction= "Ifroom" />    <ItemAndroid:id= "@+id/action_share"Android:icon= "@android:d rawable/ic_menu_share"android:orderincategory= "All"Android:title= "Share"app:showasaction= "Ifroom" />    <ItemAndroid:id= "@+id/action_settings"android:orderincategory= "+"Android:title= "Settings"app:showasaction= "Never" /></Menu>

It is also important to note that the menu button will not be displayed if the activity is not bound to Menu,toolbar, even if the three-point pattern on the right is not displayed, only after the menu is set.

Toolbar will also be used with Drawerlayout, coordinatorlayout and other controls, here are some portal for everyone to refer to:

    • Toolbar and Drawerlayout used together
    • Toolbar and Coordinatorlayout used together

The above is the simple use of toolbar introduction, the following sticker code cloud on the source, for your reference:

Demo Address

Use of the toolbar "Android-v"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.