开发者

Stop app name from flashing in title bar

开发者 https://www.devze.com 2023-01-08 06:55 出处:网络
I made a custom title bar for my app which works. However, when the app first loads, the app name is displayed in the title bar for a moment before my text appears. How do I stop this from happening?

I made a custom title bar for my app which works. However, when the app first loads, the app name is displayed in the title bar for a moment before my text appears. How do I stop this from happening?

I set my custom title bar in my main activity here:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
    setContentView(R.layout.main);
    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.mytitle);

mytitle.xml is where I set my text:

<?xml version="1.0" encoding="utf-8"?>
 <TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="开发者_Python百科@+id/myTitle"
    android:text="my text"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:textAppearance="?android:attr/textAppearance"
    />

My title bar background doesn't flash, just the text.

Thanks.


Basic explanation: when launching an application, to respond as quickly as possible to the user, Android first shows a "preview" window of the app before it even starts bringing up its process. It does this by taking the information available in the app's AndroidManifest.xml and creating a window that looks like however an empty activity for it would appear. Thus, you should set the theme in the manifest to something that matches as closely as possible what your actual UI looks like.

For the case of a custom title, there isn't much you can do, because what the title will look like depends on what your code actually does and none of your code is running yet. The best you can probably do is use android:theme="@android:style/Theme.NoTitleBar" so that the preview window doesn't try to show any title bar at all... of course in your activity you will need to clear this out by calling setTheme(android.R.style.Theme) to go back to the default theme.

To elaborate just a bit on where in AndroidManifest.xml this needs to be placed:

<application 
    android:label="@string/app_name"
    android:theme="@android:style/Theme.NoTitleBar" >


Another solution could be set the tag:

android:label=""

In all the activities in the Manifest file.


Well, you could try simply setting the title bar first, before you set the main content.

public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
  getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.mytitle);
  setContentView(R.layout.main);
0

精彩评论

暂无评论...
验证码 换一张
取 消