Android toolbar using the top title bar + Back key

Source: Internet
Author: User

Recently, there is a title bar in Android that has a back button that can be used to perform functions such as backwards.

Well, just can use toolbar to achieve

On code: Activity_main.xml

<?XML version= "1.0" encoding= "Utf-8"?><Android.support.design.widget.CoordinatorLayoutxmlns: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"Tools:context=". Mainactivity ">    <Android.support.design.widget.AppBarLayoutAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:theme= "@style/apptheme.appbaroverlay">        <Android.support.v7.widget.ToolbarAndroid:id= "@+id/toolbar"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:background= "? Attr/colorprimary"App:popuptheme= "@style/apptheme.popupoverlay">            <TextViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:layout_centerinparent= "true"android:layout_gravity= "Center"Android:text= "Settings"android:textsize= "22SP" />        </Android.support.v7.widget.Toolbar>    </Android.support.design.widget.AppBarLayout></Android.support.design.widget.CoordinatorLayout>

Here you need to quote styles.xml to add the style inside

<Resources>    <!--Base application theme. -    <stylename= "Apptheme"Parent= "Base.Theme.AppCompat.Light">        <!--Customize your theme here. -        <Itemname= "Colorprimary">@color/colorprimary</Item>        <Itemname= "Colorprimarydark">@color/colorprimarydark</Item>        <Itemname= "Coloraccent">@color/coloraccent</Item>    </style>    <stylename= "Apptheme.noactionbar">        <Itemname= "Windowactionbar">False</Item>        <Itemname= "Windownotitle">True</Item>    </style>    <stylename= "Apptheme.appbaroverlay"Parent= "ThemeOverlay.AppCompat.Dark.ActionBar" />    <stylename= "Apptheme.popupoverlay"Parent= "ThemeOverlay.AppCompat.Light" /></Resources>    

Final Main program:

 Packageaction.sun.com.testtoobar1;ImportAndroid.os.Bundle;ImportAndroid.support.design.widget.FloatingActionButton;ImportAndroid.support.design.widget.Snackbar;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.support.v7.widget.Toolbar;ImportAndroid.view.View;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.view.Window;ImportAndroid.view.WindowManager;ImportAndroid.widget.Toast; Public classMainactivityextendsappcompatactivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); /*GetWindow (). SetFlags (WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSC    Reen); Set Full Screen*/Setcontentview (R.layout.activity_main); Toolbar Toolbar=(Toolbar) Findviewbyid (R.id.toolbar); //Be sure to add, in order to remove the title text itselfToolbar.settitle (""); //Initialize toolbarSetsupportactionbar (toolbar); //the small arrow on the left (note the effect is required after Setsupportactionbar (toolbar))Toolbar.setnavigationicon (R.mipmap.back); } @Override Public BooleanOncreateoptionsmenu (Menu menu) {//inflate the menu; This adds items to the action bar if it is present.Toast.maketext (mainactivity. This, "Menu selected", Toast.length_short). Show (); //Initialize menu options to the right//getmenuinflater (). Inflate (R.menu.menu_main, menu);        return true; } @Override Public Booleanonoptionsitemselected (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 the parent activity in Androidmanifest.xml.        intID =Item.getitemid (); Toast.maketext (mainactivity. This, "Select Back button =" +ID, Toast.length_short). Show (); //noinspection simplifiableifstatement        return Super. onoptionsitemselected (item); }}

By the end of this code, you will be able to accomplish the desired effect.

Android toolbar using the top title bar + Back key

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.