Android 闪屏活动背景色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26155028/
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 11:05:50  来源:igfitidea点击:

Splash screen activity background color

androidxamarinsplash-screen

提问by Ludwo

I have problem with my splash screen on Android. Splash screen is displayed to the user during long application startup but activity background is always black. I mean background bitmap (splash image) is visible, but background is black instead of white. I'm using PNG image with transparency.

我在 Android 上的启动画面有问题。在长时间的应用程序启动期间向用户显示启动画面,但活动背景始终为黑色。我的意思是背景位图(初始图像)是可见的,但背景是黑色而不是白色。我正在使用具有透明度的 PNG 图像。

What I have:

我拥有的:

  1. PNG splash screen image with transparent background
  2. Splash screen activity
  1. 具有透明背景的 PNG 闪屏图像
  2. 启动画面活动
    [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. Theme style for splash screen activity in resources/values/styles.xml
  1. resources/values/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>
  1. Splash drawable in resources/drawable/splash_centered.xml
  1. 在 resources/drawable/splash_central.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 -->

Problem:As you can see, I'm using Theme.Holo.Light as parent theme and I'm using it in the rest of my app. Holo light is using white background. This white background is not applied on SplashActivity background. SplashActivity background is always black. Background bitmap (splash image) is visible, but background is black instead of white.I'm using PNG image with transparency.

问题:如您所见,我使用 Theme.Holo.Light 作为父主题,并且在我的应用程序的其余部分中使用它。全息光使用白色背景。此白色背景不适用于 SplashActivity 背景。SplashActivity 背景始终为黑色。 背景位图(初始图像)是可见的,但背景是黑色而不是白色。我正在使用具有透明度的 PNG 图像。

Question:How to set default Holo.Light theme background color (white) on the SplashScreen activity?

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

Note:I'm using Xamarin.Android, but styling is common for Android platform. Android version 4 and above.

注意:我使用的是 Xamarin.Android,但样式对于 Android 平台很常见。Android 4 及以上版本。

回答by Jon Canning

In resources/drawable/splash_centered.xml, instead of the bitmap use a layer-list

在 resources/drawable/splash_central.xml 中,使用图层列表代替位图

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

回答by Shibbs

This is how I was able to get white background splash (logo centred) in Xamarin.

这就是我能够在 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)));
    }
}

with Theme.Splash as:

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

and splash.axml code (Theme.Light.NoTitleBar) as:

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

NB: There is a slight delay in the splash png (logo) to come up, but its still acceptable, better than the black background.

注意:飞溅 png(标志)的出现有一点延迟,但它仍然可以接受,比黑色背景更好。

回答by Yang

set android:drawable="@color/colorWhite" to item.

将 android:drawable="@color/colorWhite" 设置为 item。

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