The common way to customize titlebar is to create a new titlebar topic in the values folder, for example:
<? XML version = "1.0" encoding = "UTF-8"?>
<Resources>
<Style name = "titlebarbackgroundstyle">
<Item name = "Android: background"> @ drawable/titlebar </item>
</Style>
<Style name = "theme. mycustomtheme" parent = "Android: Theme">
<Item name = "Android: windowtitlebackgroundstyle"> @ style/titlebarbackgroundstyle </item>
<Item name = "Android: windowtitlesize"> 45dp </item>
</Style>
</Resources>
Then add a custom topic for the activity in the manifest file,
<Activity Android: Name = ". mytitlebar"
Android: Label = "@ string/app_name"
Android: theme = "@ style/theme. mycustomtheme" // note the following:
In this way, the custom titlebar is basically finished.
To declare a statement without a title bar, simply add a statement to the activity.
Requestwindowfeature (window. feature_no_title );
HoweverCodeDefine an activity without a title bar, or rewrite the titlebar through a custom topic. When the app is loaded for the first time, the system will always flash the app_name before running the defined titlebar behavior. This is a strange behavior. So how to remove app_name?
First, let's take a look at the basic principle of Android app running: when the system runs an application, in order to respond to the user as soon as possible, Android first presents the user with a "preview" Window of the app, this interface is even earlier than the actual process of the application. The information in the preview "preview" window is obtained from the manifest file of the app. Therefore, a titlebar with app_name is always displayed when the app is running, regardless ofProgramHow to define our titlebar. So we can solve the problem.
First, we can add a topic without a title bar to our main activity in the manifest file of the app.
Android: theme ="@ Android: style/theme. notitlebar"
Declare that our app does not need titlebar at all, so that the app will not flash the titlebar with app_name for the first load. This ratio
Requestwindowfeature (window. feature_no_title );
It is much easier. But the question is, what if I want the app to display my custom title bar?
The solution is to use
Settheme (Android. R. style. theme );
Return to the title bar.
Note that the settheme (Android. R. style. Theme) statement must be placed at the beginning of the oncreat () statement. Otherwise, an error is reported. For example:
-
- Protected VoidOncreate (bundle savedinstancestate ){
- // Todo auto-generated method stub
-
- Settheme (R. style. mycustomtheme );// Pay attention to the position of this sentence
-
- Super. Oncreate (savedinstancestate );
-
- Requestwindowfeature (window. feature_custom_title );
-
- Setcontentview (R. layout. progressbar_test );
- Getwindow (). setfeatureint (window. feature_custom_title,// Pay attention to the order
-
- R. layout. Title); fashion Java-JavaScript mogujie.com women's clothing
In this way, the system will directly run the title bar we defined, instead of temporarily rendering app_name and then presenting our title bar features.