启动画面活动背景色

23

我的Android应用程序启动时有一个闪屏问题。虽然背景位图(闪屏图片)可见,但背景总是黑色的,而不是白色的。我使用带有透明度的PNG图像。

我拥有的内容:

  1. 带有透明背景的PNG闪屏图片
  2. 闪屏Activity
    [Activity(MainLauncher = true, Theme = "@style/Theme.Splash", NoHistory = true)]
    public class SplashScreen : Activity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            // Do your app initialization here
            // Other long running stuff

            // Run app when done
            StartActivity(typeof(MainForm));
        }
    }
  1. 在资源/值/styles.xml中为启动画面活动设置主题风格
    <resources>
      <style name="Theme.Splash" parent="@android:style/Theme.Holo.Light">
        <item name="android:windowBackground">@drawable/splash_centered</item>
        <item name="android:windowNoTitle">true</item>
      </style>
    </resources>
  • 在资源目录下的drawable文件夹中,有一个名为splash_centered.xml的Splash drawable。
    <bitmap xmlns:android="http://schemas.android.com/apk/res/android"
        android:src="@drawable/splash"
        android:gravity="center"
        android:background="@color/white"> <!-- this is ignored -->

问题: 如您所见,我正在使用Theme.Holo.Light作为父主题,并在我的应用程序的其余部分中使用它。 Holo light使用白色背景。 但是,SplashActivity背景上没有应用此白色背景。 SplashActivity的背景始终是黑色的。 背景位图(启动画面图像)可见,但背景是黑色而不是白色。 我正在使用带有透明度的PNG图像。

问题: 如何在SplashScreen活动中设置默认的Holo.Light主题背景颜色(白色)?

注意: 我正在使用Xamarin.Android,但样式适用于Android平台。 Android版本4及以上。


你在启动画面中设置内容视图了吗? - Niko Adrianus Yuwono
不是的。这只是使用样式完成的,没有布局。 - Ludwo
@Ludwo,你搞定了吗? - Alex.F
不,背景仍然是黑色。 - Ludwo
3个回答

53

在resources/drawable/splash_centered.xml中,使用一个layer-list替代位图

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
  <item>
    <shape android:shape="rectangle">
      <solid android:color="@android:color/white" />
    </shape>
  </item>
  <item>
    <bitmap android:gravity="center" android:src="@drawable/splash" />
  </item>
</layer-list>

效果很棒,实际上比使用自定义活动的解决方案更好,因为主题可以更早地显示图像。 - Mando
这个应该放在哪里? - Ian Warburton
@IanWarburton 在 resources/drawable/splash_centered.xml 中。原问题的第四步,使用 layer-list 代替位图。 - minnow
那个完美地运作了...我想知道是否有人可以分享一些关于学习UI相关内容的路径,比如图层列表、矢量图像等等...有太多东西了,很容易让人感到困惑。 - Shachi

11

这是我如何在 Xamarin 中获得白色背景闪屏(标志居中)的方法。

[Activity (Theme= "@style/Theme.Splash", MainLauncher=true, NoHistory=true)]            
public class SplashActivity : Activity
{
    protected override void OnCreate (Bundle bundle)
    {
        base.OnCreate (bundle);
        SetContentView (Resource.Layout.splash);
        ThreadPool.QueueUserWorkItem (o => LoadActivity ());
        // Create your application here
    }
    private void LoadActivity() {
        Thread.Sleep (1000); // Simulate a long pause
        RunOnUiThread (() => StartActivity (typeof(MainActivity)));
    }
}

使用Theme.Splash作为:

<resources>
  <style name="Theme.Splash" parent="@android:style/Theme.Light">
    <item name="android:colorBackground">@android:color/white</item>
    <item name="android:windowNoTitle">true</item>
  </style>
</resources>

将splash.axml的代码更改为(Theme.Light.NoTitleBar):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:minWidth="25px"
    android:minHeight="25px"
    android:gravity="center">
    <ImageView
        android:src="@drawable/splash"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView1"
        android:layout_gravity="center" />
</LinearLayout>

注意:启动画面的png(标志)稍有延迟,但仍可接受,比黑色背景要好。

我的背景位图(闪屏图片)是可见的,但背景颜色是黑色而不是白色。这就是问题所在。我正在使用带透明度的PNG图像。我更新了我的问题以更加清晰明了。 - Ludwo
通过上述解决方法,您可以在 SplashActivity 中使透明的启动画面拥有白色背景。 - Shibbs
我在网上找到的唯一有效的解决方案。谢谢。 - phillipwei
它确实可以工作,但在下一个活动开始时会导致相当大的卡顿。我建议只加载白色背景,然后在主活动中创建应用程序时显示标志。这对我来说是这种情况,因为我们的应用程序需要5到10秒钟才能加载,并且我们已经显示了标志。 - Gandalf458

1
将 android:drawable="@color/colorWhite" 设置为项目的属性。
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

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

    <item>
        <bitmap
            android:gravity="center"
            android:src="@drawable/splash" />
    </item>
</layer-list>

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