Android 5.0 (L) ToolBar (alternative Actionbar) Reality (iv)

Source: Internet
Author: User

After three days of rest, I came back and we went on to discuss toolbar.

Before assuming that you are stepping down the sequence, then you should note that Mainactivity is said to have been warned. Because of them, we declare toolbar objects. But it didn't work. Assuming you want to implement, then the menu function, necessary to use.

Using the menu with toolbar is also very easy. Just rewrite Oncreateoptionsmenu () and onoptionsitemselected () in mainactivity to be able.

Such a method already has a long history, there is no more to say.

Looking back at Actionbar, the previous Actionbar, when implementing the menu, assumes that the machine that encounters the virtual key displays the menu in the upper-right corner of the activity. If you encounter a machine with an entity key, it will not. Users need to manually touch the menu key Talent activation menu.

This is in fact not quite reasonable as a user. It is very likely that there are menus in the app.

Therefore, in toolbar, this problem has been very well repaired. The effect is that, regardless of the existence of entity keys, the upper right corner will show the menu, and to achieve this effect is also easy, only need the following sentence:

Setsupportactionbar (toolbar);

To devolve on:


A careful friend can find. The top toolbar has changed the style, so how does it come true?

There are two ways to do this:

1) define the style in each layout file with toolbar;

2) point to the Unified app theme style in Androidmanifest.

The former needn't say much. Just specify the value of the specific field in the layout file. Specifically, the latter is here.

First of all, in the layout file, in addition to the location of toolbar, title text and ID, do not give no matter what other values, for example:

<android.support.v7.widget.toolbar        android:id= "@+id/demo_toolbar"        android:layout_width= "fill_parent "        android:layout_height=" wrap_content "        app:title=" @string/hello_world "/>
And then. To create a theme.xml in the values directory, define the theme style in which to list the following program:

<?xml version= "1.0" encoding= "Utf-8"? ><resources xmlns:android= "http://schemas.android.com/apk/res/ Android >    <!--default Blue style--    <style name= "Defaultbluetheme" parent= "@style/ Theme.AppCompat.Light.NoActionBar ">        <!--toolbar style--        <item name=" Toolbarstyle "> @style/ defaultbluetoolbar</item>    </style>    <!--default toolbar style--    <style name= " Defaultbluetoolbar "parent=" @style/widget.appcompat.toolbar ">        <item name=" Android:background ">@ color/default_blue</item>        <item name= "titletextappearance" > @style/toolbartitletextstyle</ Item>    </style></resources>
The color values for the reference are as follows. Defined in Color.xml:

    <color name= "Default_blue" > #33B5E5 </color>    <color name= "Default_white" > #FFFFFF </color >
This achieves the fruit of effect.

This, in the next chapter, we will explore the use of sidebar on the development.

Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

Android 5.0 (L) ToolBar (alternative Actionbar) Reality (iv)

Related Article

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.