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