A simple example of Android development toolbar

Source: Internet
Author: User
Tags xmlns

Unlike Actionbar, toolbar is more like a general view element, can be placed anywhere in the view tree system, can be applied to animations, can be scrolled along with ScrollView, and can interact with other view in the layout. Of course, you can also replace Actionbar with toolbar, just call Activity.setactionbar ().

Although toolbar has been out for a long time, it is still necessary to make a brief introduction,

First on the layout

<android.support.design.widget.appbarlayout
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
Android:theme= "@style/apptheme.appbaroverlay" >

<android.support.v7.widget.toolbar
Android:id= "@+id/toolbar"
Android:layout_width= "Match_parent"
Android:layout_height= "Attr/actionbarsize"?
Android:background= "Attr/colorprimary"?
App:theme= "@style/apptheme.appbaroverlay"
>
<imageview
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:layout_alignparentleft= "true"
Android:layout_centerinparent= "true"
android:layout_marginleft= "15DP"
android:background= "@mipmap/icon_index_search"/>

<textview
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:layout_centerinparent= "true"
android:text= "Search"
Android:textcolor= "#956C28"
Android:textsize= "16sp"/>



</android.support.v7.widget.Toolbar>

</android.support.design.widget.AppBarLayout>
Screen shot 2016-04-22 16.38.12

The above layout presents this effect, can be customized inside, you can also use the menu to write the control, if you need to hide the toolbar in the case of the need to wrap a layer outside appbarlayout


<menu xmlns:android= "Http://schemas.android.com/apk/res/android"
xmlns:app= "Http://schemas.android.com/apk/res-auto" >
<item
Android:id= "@+id/action_location"
android:orderincategory= "0"
android:title= "Positioning"
android:icon= "@mipmap/icon_index_location"
app:showasaction= "Ifroom"/>


</menu>
Here you can specify which controls toolbar displays, App:showasaction properties can specify whether the icon is displayed on the toolbar or in the menu, and you can test it yourself.

The following goes into the core code section,

Toolbar Toolbar = (Toolbar) Findviewbyid (R.id.toolbar);
Toolbar.setnavigationicon (R.mipmap.icon_index_menu);
Setsupportactionbar (toolbar);

You can set the results we need by toolbar objects, such as icon, title, subtitle, etc.


@Override
public boolean Oncreateoptionsmenu (Menu menu) {
Getmenuinflater (). Inflate (R.menu.main, menu);
return true;
}

@Override
public boolean onoptionsitemselected (MenuItem item) {
int id = item.getitemid ();
if (id = = r.id.action_location) {

return true;
}

return super.onoptionsitemselected (item);
}
Rewrite the above two methods to load the menu button on the toolbar, and then set the listener, and it's over. Here's what you need to pay attention to, in the layout file can also set icon, title, etc., but you need to add a custom attribute to the namespace


<pre class= "Hljs xml" ><code class= "xml" >xmlns:toolbar= "Http://schemas.android.com/apk/res-auto" </ Code>

Then set various properties by Toolbar:title= "".

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.