Because only the application name is displayed in the default title bar, if you want to add some buttons or change the style on the title bar, you have to change it.CodeThe procedure is as follows:
- Add several lines of code to the oncreate method in the Activity Class
Public class mainactivity extends activity {@ override protected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); // set the title bar requestwindowfeature (window. feature_custom_title); setcontentview (R. layout. activity_main); getwindow (). setfeatureint (window. feature_custom_title, R. layout. title_bar); // update the font size of the button in the title bar. Button btnmanage = (button) findviewbyid (R. id. btn_manage); btnmanage. settextsize (10 );
These two lines are used:
Requestwindowfeature (window. feature_custom_title );
This line is placed before setcontentview.
Getwindow (). setfeatureint (window. feature_custom_title, R. layout. title_bar );
This line is placed behind setcontentview, where R. layout. title_bar is our custom layout.
- Create a custom layout and replace the default title bar of the framework,
<? XML version = "1.0" encoding = "UTF-8"?> <Relativelayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: gravity = "center_vertical" Android: orientation = "horizontal"> <textview Android: Id = "@ + ID/text_app_name" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: layout_margintop = "2dip" Android: layout_marginleft = "10dip" Android: textcolor = "# fff" Android: text = "@ string/app_name"/> <button Android: id = "@ + ID/btn_manage" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: layout_gravity = "right" Android: layout_margintop = "4dip" Android: layout_alignparentright = "true" Android: paddingbottom = "5dip" Android: paddingtop = "0dip" Android: textcolor = "#006400" Android: TEXT = "@ string/btn_manage"/> </relativelayout>
-
- In this way, let's take a look at the effect.