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

时间:2019-12-29 09:03:33

标签: image xamarin.android background

我要使以下图像成为整个Xamarin.Android应用程序的背景:

enter image description here

我引用了Base application themestyles.xml中的图片:

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

背景适用于所有应用程序,但是图像具有深色背景:

enter image description here

数字51周围还有些奇怪的东西。

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

enter image description here

如何修复android:windowBackground属性,将背景设置为黑色,这样我就不必在每次活动的android:background="@drawable/ic_app_background_image"中都放置LinearLayout

1 个答案:

答案 0 :(得分:0)

您可以尝试以下方法:

1。在bg.xml文件夹(bg.xml)中以drawable创建新文件名

 <?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。在colors.xml中定义背景颜色

   <color name="window_background">#F5F5F5</color>
   <color name="splash_background">#FFFFFF</color> 

3。在MyTheme.Splash中定义新样式style.xml,并在项目bg中使用android:windowBackground

 <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。活动中的使用

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

更新

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

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

结果是:

enter image description here

有关更多详细信息,您可以检查:https://docs.microsoft.com/en-us/xamarin/android/user-interface/splash-screen