android:windowBackground 属性 使背景变黑 (Xamarin.Android)

android:windowBackground property makes background black (Xamarin.Android)

我想将下图作为整个 Xamarin.Android 应用程序的背景:

我在styles.xml中引用了Base application theme中的图片:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    .....
    <item name="android:windowBackground">@drawable/ic_app_background_image</item>
    .....
</style>

背景适用于所有应用程序,但图像背景较暗:

此外,数字 51 周围也有一些奇怪的东西。

当我将 android:background="@drawable/ic_app_background_image" 设置为 activity 的最外层 LinearLayout 时,它工作正常,它显示的屏幕如下:

如何解决 android:windowBackground 属性 将背景设置为黑色的问题,这样我就不必在每个 [=] 的 LinearLayout 中放入 android:background="@drawable/ic_app_background_image" 43=]?

您可以尝试以下方法:

1.Create drawable 文件夹中的新文件名为 bg.xml (bg.xml)

 <?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
  <item>
    <color android:color="@color/splash_background"/>
  </item>
  <item>
    <bitmap
        android:src="@drawable/splash_logo"
        android:tileMode="disabled"
        android:gravity="center"/>
  </item>
</layer-list>

2.define colors.xml

中的背景颜色
   <color name="window_background">#F5F5F5</color>
   <color name="splash_background">#FFFFFF</color> 

3.define style.xml 中的新样式 MyTheme.Splash 并使用项目 android:windowBackground

中的 bg
 <style name="MyTheme.Splash" parent ="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowBackground">@drawable/bg</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowFullscreen">false</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowActionBar">true</item>
  </style>

4.Usage 在 Activity

   [Activity(Theme = "@style/MyTheme.Splash", MainLauncher = true, NoHistory = true)]
    public class SplashActivity : AppCompatActivity
    {
     // other code
    }

更新:

如果您想将此样式应用于所有活动,只需将以下代码添加到您的 Base 应用程序主题中即可。而且我们不需要为每个 activity 设置。例如:

  <!-- Base application theme.   parent="Theme.AppCompat.Light.DarkActionBar" -->
<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>

  <item name="android:windowBackground">@drawable/bg</item>
  <item name="android:windowNoTitle">true</item>
  <item name="android:windowFullscreen">false</item>
  <item name="android:windowContentOverlay">@null</item>
  <item name="android:windowActionBar">true</item>
</style>

结果是:

更多详情,您可以查看:https://docs.microsoft.com/en-us/xamarin/android/user-interface/splash-screen