【问题标题】:How To fix white screen on app Start up?如何修复应用程序启动时的白屏?
【发布时间】:2013-12-31 01:16:33
【问题描述】:

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

【问题讨论】:

标签: android splash-screen app-startup


【解决方案1】:

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

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

【讨论】:

  • 谢谢人,免去了很多麻烦。最初我很难找到设定点。它将是主主题块: inline `
  • 谢谢!将其放到 res 文件夹中的 style.xml 中,放入 style 标签 name="AppTheme" (或者你在 manifest 中使用的主题)
  • 为我工作。谢谢。
  • 我认为它应该被接受,因为很多开发者使用自定义主题
  • 我认为这会延迟应用启动
【解决方案2】:

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

喜欢:

<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>

并使用Activity 类代替AppCompatActivity 扩展该屏幕。

喜欢:

public class SplashScreenActivity extends Activity{

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

【讨论】:

  • 但是当主题是半透明的时候使用final ActionBar actionBar = getActionBar();会返回null
  • Pankaj - 是的,我了解到糟糕的 UI 体验是由于“预览”窗口造成的……显然,Google 有人认为默认使用白色预览窗口真的很棒。但是,在使用深色背景的应用程序中,这是一个非常糟糕的主意。解决方案是为您的应用创建自定义样式,并将“android:windowBackground”指定为您要使用的颜色。请参阅“完美预览窗口”部分cyrilmottier.com/2013/01/23/android-app-launching-made-gorgeous
  • @Hagai L 它给了我一个类似“java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this Activity”之类的错误。
  • android:theme="@android:style/Theme.Translucent.NoTitleBar" 对我不起作用,有什么建议吗?
  • @TejaDroid 要获得停止错误“java.lang.IllegalStateException”,您必须通过 android.app.Activity 扩展您的 Activity,现在您正在使用 AppCompactActivity。所以对于 AppCompactActivity 你不能使用普通的主题。
【解决方案3】:

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

首先在 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>

接下来,您将在主题中将此设置为启动活动的背景。导航到您的 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>

在您的新 SplashTheme 中,将窗口背景属性设置为您的 XML 可绘制对象。在 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>

此链接提供您想要的。一步一步的程序。 https://www.bignerdranch.com/blog/splash-screens-the-right-way/

更新:

layer-list 可以像这样更简单(它也接受用于居中徽标的矢量可绘制对象,与 &lt;bitmap&gt; 标签不同):

<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>

【讨论】:

  • 这真是太棒了,但是如何在mipmap/ic_launcher下添加文字
  • 这是最好的答案。原因是在使用上述方案时,应用的视觉启动有延​​迟。
  • 另一个修复控制位图stackoverflow.com/questions/23079355/…
  • 这应该是公认的答案,所有其他的只是让它看起来像应用程序从未从用户的角度启动。更简单的可绘制 layer-list 可以是:&lt;layer-list xmlns:android="http://schemas.android.com/apk/res/android"&gt; &lt;!-- Background color --&gt; &lt;item android:drawable="@color/white"/&gt; &lt;!-- Logo at the center of the screen --&gt; &lt;item android:drawable="@mipmap/ic_launcher" android:gravity="center"/&gt; &lt;/layer-list&gt;
  • 当我们想以编程方式更改图层列表中的背景颜色时(在您的示例中,从灰色变为蓝色),此解决方案无法解决问题。在这种情况下,首先出现灰屏一两秒,然后设置蓝色背景!
【解决方案4】:

在你的 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>

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

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

【讨论】:

    【解决方案5】:

    您应该阅读 Cyril Mottier 的这篇精彩文章:Android App launching made gorgeous

    您需要在 style.xml 中自定义您的 Theme,并避免在您的 onCreate 中自定义为 ActionBar.setIcon/setTitle/etc。

    另请参阅 Google 在 Performance Tips 上的文档。

    使用Trace ViewHierarchy Viewer 查看显示您的视图的时间:Android Performance Optimization / Performance Tuning On Android

    使用AsyncTask 显示一些视图。

    【讨论】:

      【解决方案6】:

      这是我在示例应用上的 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>
      

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

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

      【讨论】:

        【解决方案7】:

        这两个属性都有效使用其中任何一个。

            <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>
        

        【讨论】:

          【解决方案8】:

          user543 answer 是完美的

          <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>
          

          但是:

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

          【讨论】:

          • 我多次尝试了这个解决方案,因为到处都写了同样的解决方案,它无法工作--> 但是你的但是节省了我的时间我的 Activity 扩展了 AppCompatActivity 这就是为什么它不起作用我现在把它改成了活动它的工作原理谢谢你!
          【解决方案9】:

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

          android:windowBackground=""
          

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

          【讨论】:

          • 这行得通,如果将透明颜色设置为窗口会更好。 &lt;item name="android:windowBackground"&gt;@android:color/transparent&lt;/item&gt;
          • 这应该是公认的答案。不管怎样,你知道如何像 youtube 应用一样添加缩放动画吗?
          • 现在是黑色而不是白色。
          【解决方案10】:

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

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

          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>
          

          添加以下样式

          <!-- 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>
          

          在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>
          

          【讨论】:

            【解决方案11】:

            我在我的一个项目中也遇到了同样的问题。我通过在提供给初始屏幕的主题中添加一些以下参数来解决它。

            <item name="android:windowFullscreen">true</item>
            <item name="android:windowContentOverlay">@null</item>
            <item name="android:windowIsTranslucent">true</item>
            

            你可以在我写的this blog post找到原因和解决方法。希望对您有所帮助。

            【讨论】:

            • 请将详细信息直接添加到您的答案中。链接可能会失效,这就是为什么我们希望答案包含实际内容,链接只是补充。
            【解决方案12】:

            可以通过将清单中的主题设置为来修复

            <activity
                    android:name=".MySplashActivityName"
                    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>
            

            然后如果您收到
            java.lang.IllegalStateException: 您需要在此活动中使用 Theme.AppCompat 主题(或后代)。
            那么您可能需要在 MySplashActivity 中扩展 Activity 而不是 AppCompatActivity

            希望对你有帮助!

            【讨论】:

              【解决方案13】:

              您应该禁用 Instant Run android studio 设置。

              文件>设置>构建、执行、部署>即时运行取消选中此处显示的所有选项。

              注意:Instant Run导致的白屏问题仅适用于调试版本,此问题不会出现在发布版本中。

              【讨论】:

                【解决方案14】:

                白色背景是由于Android在应用程序加载到内存时启动而导致的,只需在SplashTheme下添加这2行代码即可避免。

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

                【讨论】:

                  【解决方案15】:

                  这解决了问题:

                  编辑您的styles.xml 文件:


                  粘贴下面的代码:

                  <resources>
                  
                      <!-- Base application theme. -->
                      <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
                          <!-- Customize your theme here. -->
                          <item name="android:windowFullscreen">true</item>
                          <item name="android:windowContentOverlay">@null</item>
                          <item name="android:windowIsTranslucent">true</item>
                      </style>
                  
                  </resources>
                  

                  别忘了在 AndroidManifest.xml 文件中进行修改。 (主题名称

                  注意此文件中活动的声明顺序。

                  【讨论】:

                    【解决方案16】:
                    I encountered a similar problem and to overcome it, I implemented the code below in styles, i.e res->values->styles->resource tag 
                    <item name="android:windowDisablePreview">true</item>
                    
                    Here is the whole code:
                    
                    <style name="SplashTheme" parent="Theme.AppCompat.Light.NoActionBar">
                        <item name="android:windowDisablePreview">true</item>
                    </style>
                    

                    【讨论】:

                      【解决方案17】:

                      试试下面的代码:

                      <!-- Base application theme. -->
                      <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
                          <!-- Customize your theme here. -->
                          <item name="colorPrimary">@color/colorPrimary</item>
                          <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
                          <item name="colorAccent">@color/colorAccent</item>
                          <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>
                      

                      此代码适用于我,适用于所有 Android 设备。

                      【讨论】:

                      • 不鼓励使用纯代码的答案。请单击edit 并添加一些词来总结您的代码如何解决问题,或者解释您的答案与之前的答案/答案有何不同。 From Review
                      【解决方案18】:

                      它的解决方案非常简单!

                      这个问题有三个基本原因

                      1. 您正在 onCreateVeiw 函数中执行繁重/长时间运行/复杂任务
                      2. 如果您使用的是线程。那么线程休眠时间可能会很大。
                      3. 如果您使用任何第三方库。在应用启动initialize可能会导致此问题。

                      解决方案:

                      解决方案 1:

                        Remove the Heavy Task from onCreateView() function and place it some where appropriate place.
                      

                      解决方案 2:

                        Reduce the Thread Sleep time.
                      

                      解决方案 3:

                        Remove the Third party library at app initialize at implement them with some good strategy.
                      

                      就我而言,我使用的是导致此问题的 Sugar ORM。

                      分享以改进。

                      【讨论】:

                        猜你喜欢
                        • 2023-02-06
                        • 1970-01-01
                        • 1970-01-01
                        • 2017-01-13
                        • 2020-05-28
                        • 1970-01-01
                        • 1970-01-01
                        • 1970-01-01
                        • 1970-01-01
                        相关资源
                        最近更新 更多