Android Latest Control Toolbar

Source: Internet
Author: User

Before the Android Actionbar seems to do the project has never been used, in addition to Google itself, and not many people use, the reason, mainly because actionbar is not flexible enough to be able to customize, then Goole also found this point, and then, It was born today. Toolbar,toolbar is a new control, used instead of Actionbar, it is a viewgroup, so there is good customization, for example, you can prevent layout, linearlayout, etc., now start the code bar.

In order to be compatible with the bottom version, so to use the Toolbar in the V7 package, android. Support.v7.widget.Toolbar This is the full name, first of all, add a sentence in the Gradle


And then set the theme in value to remove Actionbar

    <style name= "Appbasetheme" parent= "Theme.AppCompat.Light.NoActionBar" >        <item name= "Colorprimarydark" > #7B1FA2 </item>        <item name= "colorprimary" > #9C26B0 </item>        <item name= "coloraccent "> #ff4081 </item>        <item name=" android:textcolorprimary "> #fffdfbff </item>    </style >
And the subject of value12.


   <style name= "Apptheme" parent= "Appbasetheme" >            <item name= "Android:statusbarcolor" > #7B1FA2 </ Item>        </style>

Now we can use Toolebar.

Here is my main layout code:

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:app= "http// Schemas.android.com/apk/res-auto "xmlns:tools=" Http://schemas.android.com/tools "android:layout_width=" Match_ Parent "android:layout_height=" match_parent "android:background=" #fff "android:orientation=" vertical "Tools:co Ntext= ". Mainactivity "> <android.support.v7.widget.toolbar android:id=" @+id/id_toolbar "android:layout_width = "Match_parent" android:layout_height= "wrap_content" android:background= "? Attr/colorprimary" Android: minheight= "? attr/actionbarsize" app:title= "Try" > <linearlayout android:layout_width= "Wrap_c Ontent "android:layout_height=" wrap_content "android:layout_marginleft=" @dimen/abc_text_size_title_m                Aterial "android:gravity=" center "android:orientation=" vertical "> <textview     Android:layout_width= "Wrap_content"           android:layout_height= "Wrap_content" android:text= "linear layout"/> <checkbox Android:layout_width= "Wrap_content" android:layout_height= "wrap_content"/> </linearlayo ut> </android.support.v7.widget.Toolbar> <textview android:layout_width= "Wrap_content" and roid:layout_height= "Wrap_content" android:layout_margin= "@dimen/abc_text_size_title_material" android:text= " @string/hello_world "/></linearlayout>

Activity Code

Package Com.example.liran.workspace1;import Android.app.actionbar;import Android.app.activity;import Android.os.bundle;import Android.support.v7.app.actionbaractivity;import Android.support.v7.app.appcompatactivity;import Android.view.menu;import Android.view.menuitem;import Android.widget.textview;import Android.widget.toolbar;public class Mainactivity extends Appcompatactivity {private    Android.support.v7.widget.Toolbar Toolbar;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);         Setcontentview (R.layout.activity_main);         toolbar = (Android.support.v7.widget.Toolbar) Findviewbyid (R.id.id_toolbar);         Toolbar.setlogo (R.mipmap.ic_launcher);         Toolbar.setsubtitle ("subtitle");    Toolbar.setnavigationicon (R.mipmap.ic_launcher); } @Override public boolean onoptionsitemselected (MenuItem Item) {//Handle Action Bar item clicks here. The action bar would//automatically handle clicks on the Home/up button, so long/As you specify a parent activity in Androidmanifest.xml.        int id = item.getitemid ();        Noinspection simplifiableifstatement if (id = = r.id.action_settings) {return true;    } return super.onoptionsitemselected (item); }}

Note here that the activity is to inherit appcompatactivity, which is recommended by the latest API. The following is a description of the run effect and properties.



So it's over.


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android Latest Control Toolbar

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.