启动画面活动背景颜色

问题描述 投票:0回答:4

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

我有:

  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. 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. 资源/drawable/splash_centered.xml中的Splash可绘制对象
    <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 光使用白色背景。 此白色背景不适用于 SplashActivity 背景。 SplashActivity 背景始终为黑色。 背景位图(初始图像)可见,但背景是黑色而不是白色。我使用的是具有透明度的 PNG 图像。

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

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

android xamarin splash-screen
4个回答
53
投票

在 resources/drawable/splash_centered.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>

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)));
    }
}

主题.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>

注意:splash png(徽标)的出现略有延迟,但仍然可以接受,比黑色背景更好。


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>

0
投票

郑重声明,我在 VSC 中使用了离子/电容器扩展。那么生成的代码就有了主题。我必须将 @drawable/splash 更改为 @android:color/white,否则背景总是灰色的。

<?xml version="1.0" encoding="utf-8"?>
<!-- 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>

<style name="AppTheme.NoActionBar" parent="Theme.AppCompat.DayNight.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="android:background">@null</item>
</style>


<style name="AppTheme.NoActionBarLaunch" parent="Theme.SplashScreen">
    <!-- <item name="android:background">@drawable/splash</item> -->
    <item name="android:colorBackground">@android:color/white</item>
</style>
© www.soinside.com 2019 - 2024. All rights reserved.