Android应用启动时出现白色背景

16

每次我的应用程序启动时,都会短暂显示一个白色背景。 尽管使用了启动画面,问题仍然存在。 我想将启动屏幕默认设置为黑色而不是白色!

这是我的启动画面活动:

public class SplashActivity extends Activity {

private static String TAG = SplashActivity.class.getName();
private static long SLEEP_TIME = 1;    // Sleep for some time

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);    // Removes title bar
    setContentView(R.layout.splash);

    // Start timer and launch main activity
    IntentLauncher launcher = new IntentLauncher();
    launcher.start();
}

private class IntentLauncher extends Thread {

    /**
      * Sleep for some time and than start new activity.
      */ 
    @Override
    public void run() {
        try {
            // Sleeping
            Thread.sleep(SLEEP_TIME*1000);
        } catch (Exception e) {
            Log.e(TAG, e.getMessage());
        }

        // Start main activity
        Intent intent = new Intent(SplashActivity.this, MainActivity.class);
        SplashActivity.this.startActivity(intent);
        SplashActivity.this.finish();
    }

}

@Override
protected void onPause() {
    super.onPause();
    overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
}
}
7个回答

31
在 styles.xml 文件中,在 AndroidManifest.xml 中指定的主题中添加以下行:
<item name="android:windowBackground">@android:color/black</item>

如果我想要以这种样式显示一个ImageView呢? - grantespo
这就是我一直在寻找的。 - KlevisGjN
我不得不扩展Activity而不是AppCompatActivity,然后将我的主题的父级设置为"android:Theme.Material.Light.NoActionBar",这样才能让它正常工作。 - linker

11

在你的清单文件中使用这个标签:

android:theme="@android:style/Theme.NoTitleBar.Fullscreen"

而不是:

this.requestWindowFeature(Window.FEATURE_NO_TITLE);

1
所有答案中,这个是唯一一个对我有用的!谢谢! - Hitokage

6

在drawable文件夹中创建你自己的starting_screen.xml文件。

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape>
            <!-- black background -->
            <solid android:color="@color/colorBlack" />
        </shape>
    </item>
    <!-- launcher icon -->
    <item android:drawable="@mipmap/ic_launcher_foreground" android:height="150dp" android:width="150dp" android:gravity="center" />
</layer-list>

然后将此添加到您的样式中

<item name="android:windowBackground">@drawable/starting_screen</item>

现在,每次启动应用程序时,都会出现一个带有您的启动器图标的黑屏幕。


4
如果您想将启动屏幕前的白屏更改为黑色,只需更改SplashActivity的主题而不是整个应用程序的主题。
您可以使用以下代码:
<activity
    android:name="your.package.SplashActivity"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.Holo.NoActionBar.Fullscreen" >
       <intent-filter>
           <action android:name="android.intent.action.MAIN" />
           <category android:name="android.intent.category.LAUNCHER" />
       </intent-filter>
</activity>

希望这可以帮助到您。

2
在 styles.xml 文件中,为 SplashActivity 应用的主题中添加以下行:
<item name="android:windowBackground">@drawable/background</item>

其中@drawable/background是您为启动屏幕应用的背景,或者也可以是您需要的任何颜色。


2

您可以使用自定义主题更改起始窗口的背景颜色。请参见样式和主题以获取示例。


0

当您的活动启动时,您已经设置了splash.xml。更改它,将父背景更改为黑色或任何您想要的颜色。这将起作用,或将splash.xml主题更改为holo_dark或任何其他主题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接