如何更改窗口的背景色?

时间:2019-07-05 19:05:32

标签: xamarin xamarin.forms xamarin.android

我需要将窗口的背景色更改为灰色,而不是默认的白色。我目前无法做到。

最后一次尝试是styles.xml文件中的以下尝试:

<?xml version="1.0" encoding="utf-8" ?>
<resources>

  <style name="MainTheme" parent="MainTheme.Base">
  </style>
  <!-- Base theme applied no matter what API -->
  <style name="MainTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
    <!--If you are using revision 22.1 please use just windowNoTitle. Without android:-->
    <item name="windowNoTitle">true</item>
    <!--We will be using the toolbar so no need to show ActionBar-->
    <item name="windowActionBar">false</item>
    <!-- Set theme colors from http://www.google.com/design/spec/style/color.html#color-color-palette -->
    <!-- colorPrimary is used for the default action bar background -->
    <item name="colorPrimary">#000000</item>
    <!-- colorPrimaryDark is used for the status bar -->
    <item name="colorPrimaryDark">#000000</item>
    <!-- colorAccent is used as the default value for colorControlActivated
         which is used to tint widgets -->
    <item name="colorAccent">#FF4081</item>
    <item name="android:windowBackground">#EEEEEE</item>
    <!-- You can also set colorControlNormal, colorControlActivated
         colorControlHighlight and colorSwitchThumbNormal. -->
    <item name="windowActionModeOverlay">true</item>

    <item name="android:datePickerDialogTheme">@style/AppCompatDialogStyle</item>
  </style>

  <style name="AppCompatDialogStyle" parent="Theme.AppCompat.Light.Dialog">
    <item name="colorAccent">#FF4081</item>
  </style>
</resources>

我添加了这个:

<item name="android:windowBackground">#EEEEEE</item>

但是此错误在构建时显示:

不允许使用颜色类型(位于“ android:windowBackground”,值为“ #EEEEEE”)。

活动具有以下属性:

[Activity(Label = "MyApp", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]

有什么帮助吗?

问候 海梅

1 个答案:

答案 0 :(得分:0)

在Resources / values / colors.xml中定义颜色

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <color name="window_background_color">#EEEEEE</color>
</resources>

和styles.xml

<item name="android:windowBackground">@color/window_background_color</item>