How does Android prevent app_name from flashing in the title bar?

Source: Internet
Author: User

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:

  1. Protected VoidOncreate (bundle savedinstancestate ){
  2. // Todo auto-generated method stub
  3. Settheme (R. style. mycustomtheme );// Pay attention to the position of this sentence
  4. Super. Oncreate (savedinstancestate );
  5. Requestwindowfeature (window. feature_custom_title );
  6. Setcontentview (R. layout. progressbar_test );
  7. Getwindow (). setfeatureint (window. feature_custom_title,// Pay attention to the order
  8. 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.


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.