Android 如何修复应用程序启动时的白屏?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20546703/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:01:30  来源:igfitidea点击:

How To fix white screen on app Start up?

androidsplash-screenapp-startup

提问by Anonymous

I have an android app which displays a white screen for 2 seconds on startup. My other apps don't do this, but this one does. I have also implemented a splashscreen with the hope that it would fix this. Should I increase my splash screen sleep time? Thanks.

我有一个 android 应用程序,它在启动时显示白屏 2 秒。我的其他应用程序不会这样做,但这个应用程序会。我还实现了一个闪屏,希望它能解决这个问题。我应该增加启动画面的睡眠时间吗?谢谢。

采纳答案by user543

Just mention the transparent theme to the starting activity in the AndroidManifest.xml file.

只需在 AndroidManifest.xml 文件中提及启动活动的透明主题即可。

Like:

喜欢:

<activity
        android:name="first Activity Name"
        android:theme="@android:style/Theme.Translucent.NoTitleBar" >
 <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
 </activity>

and extend that screen with Activityclass in place of AppCompatActivity.

并用Activityclass 代替AppCompatActivity.

like :

喜欢 :

public class SplashScreenActivity extends Activity{

  ----YOUR CODE GOES HERE----
}

回答by ireq

Put this in a custom style and it solves all the problems. Using the hacky translucent fix will make your task bar and nav bar translucent and make the splashscreen or main screen look like spaghetti.

把它放在自定义样式中,它解决了所有问题。使用 hacky 半透明修复将使您的任务栏和导航栏半透明,并使启动画面或主屏幕看起来像意大利面条。

<item name="android:windowDisablePreview">true</item>

<item name="android:windowDisablePreview">true</item>

回答by Abhi

enter image description here

在此处输入图片说明

Like you tube.. initially they show icon screen instead of white screen. And after 2 seconds shows home screen.

就像你管..最初他们显示图标屏幕而不是白屏。2 秒后显示主屏幕。

first create an XML drawable in res/drawable.

首先在 res/drawable 中创建一个 XML drawable。

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:drawable="@color/gray"/>

    <item>
        <bitmap
            android:gravity="center"
            android:src="@mipmap/ic_launcher"/>
    </item>

</layer-list>

Next, you will set this as your splash activity's background in the theme. Navigate to your styles.xml file and add a new theme for your splash activity

接下来,您将在主题中将此设置为启动活动的背景。导航到您的 styles.xml 文件并为您的启动活动添加一个新主题

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
    </style>

    <style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
        <item name="android:windowBackground">@drawable/background_splash</item>
    </style>

</resources>

In your new SplashTheme, set the window background attribute to your XML drawable. Configure this as your splash activity's theme in your AndroidManifest.xml:

在新的 SplashTheme 中,将 window background 属性设置为 XML drawable。在您的 AndroidManifest.xml 中将此配置为您的启动活动主题:

<activity
    android:name=".SplashActivity"
    android:theme="@style/SplashTheme">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

This link gives what you want. step by step procedure. https://www.bignerdranch.com/blog/splash-screens-the-right-way/

这个链接给了你想要的。一步一步的程序。 https://www.bignerdranch.com/blog/splash-screens-the-right-way/

UPDATE:

更新:

The layer-listcan be even simpler like this (which also accepts vector drawables for the centered logo, unlike the <bitmap>tag):

layer-list甚至可以这样简单(其还接受居中标志矢量图形内容不同于<bitmap>标签):

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Background color -->
    <item android:drawable="@color/gray"/>

    <!-- Logo at the center of the screen -->
    <item
        android:drawable="@mipmap/ic_launcher"
        android:gravity="center"/>
</layer-list>

回答by Tarun

Make a style in you style.xml as follows :

在 style.xml 中创建一个样式,如下所示:

<style name="Theme.Transparent" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowActionBar">false</item>
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowIsTranslucent">true</item>
</style>

and use it with your activity in AndroidManifest as:

并将其与您在 AndroidManifest 中的活动一起使用:

<activity android:name=".ActivitySplash" android:theme="@style/Theme.Transparent">

回答by fllo

You should read this great post by Cyril Mottier: Android App launching made gorgeous

您应该阅读 Cyril Mottier 的这篇很棒的文章:Android 应用程序启动变得华丽

You need to customise your Themein style.xml and avoid to customise in your onCreateas ActionBar.setIcon/setTitle/etc.

你需要Theme在 style.xml 中自定义你的,避免在你的onCreateActionBar.setIcon/setTitle/etc 中自定义。

See also the Documentation on Performance Tipsby Google.

另请参阅Google 的性能提示文档。

Use Trace Viewand Hierarchy Viewerto see the time to display your Views: Android Performance Optimization/ Performance Tuning On Android

使用Trace ViewHierarchy Viewer查看显示视图的时间:Android 性能优化/ Android 性能调优

Use AsyncTaskto display some views.

使用AsyncTask显示的一些看法。

回答by luiscosta

This is my AppTheme on an example app:

这是我在示例应用程序上的 AppTheme:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:windowIsTranslucent">true</item>
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

As you can see, I have the default colors and then I added the android:windowIsTranslucentand set it to true.

如您所见,我使用了默认颜色,然后添加了android:windowIsTranslucent并将其设置为true.

As far as I know as an Android Developer, this is the only thing you need to set in order to hide the white screen on the start of the application.

据我所知,作为一名 Android 开发人员,这是您唯一需要设置的,以便在应用程序启动时隐藏白屏。

回答by Sohail Zahid

Both properties worksuse any one of them.

这两个属性作品都使用其中的任何一个。

    <style name="AppBaseThemeDark" parent="@style/Theme.AppCompat">
            <!--your other properties -->
            <!--<item name="android:windowDisablePreview">true</item>-->
            <item name="android:windowBackground">@null</item>
            <!--your other properties -->
    </style>

回答by Igor Fridman

The user543 answeris perfect

user543答案是完美的

<activity
        android:name="first Activity Name"
        android:theme="@android:style/Theme.Translucent.NoTitleBar" >
 <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
 </activity>

But:

但:

You'r LAUNCHER Activitymust extands Activity, not AppCompatActivityas it came by default!

您的LAUNCHER Activity必须扩展Activity,而不是默认情况下的AppCompatActivity

回答by Rajesh.k

The white background is coming from the Apptheme.You can show something useful like your application logo instead of white screen.it can be done using custom theme.in your app Theme just add

白色背景来自 Apptheme。您可以显示一些有用的东西,例如您的应用程序徽标而不是白色屏幕。可以使用自定义主题来完成。在您的应用程序中,只需添加主题即可

android:windowBackground=""

attribute. The attribute value may be a image or layered list or any color.

属性。属性值可以是图像或分层列表或任何颜色。

回答by Rocky

Below is the link that suggests how to design Splash screen. To avoid white/black background we need to define a theme with splash background and set that theme to splash in manifest file.

下面是建议如何设计启动画面的链接。为了避免白色/黑色背景,我们需要定义一个带有飞溅背景的主题,并将该主题设置为在清单文件中飞溅。

https://android.jlelse.eu/right-way-to-create-splash-screen-on-android-e7f1709ba154

https://android.jlelse.eu/right-way-to-create-splash-screen-on-android-e7f1709ba154

splash_background.xml inside res/drawable folder

在 res/drawable 文件夹中的 splash_background.xml

<?xml version=”1.0" encoding=”utf-8"?>
 <layer-list xmlns:android=”http://schemas.android.com/apk/res/android">

 <item android:drawable=”@color/colorPrimary” />

 <item>
 <bitmap
 android:gravity=”center”
 android:src=”@mipmap/ic_launcher” />
 </item>

</layer-list>

Add below styles

添加以下样式

<!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
    <!-- Splash Screen theme. -->
    <style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
        <item name="android:windowBackground">@drawable/splash_background</item>
    </style>

In Manifest set theme as shown below

在 Manifest 中设置主题如下图

<activity
            android:name=".SplashActivity"
            android:theme="@style/SplashTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
    </activity>