首页 > 解决方案 > 如何在 Xamarin、Android 项目中设置主题、样式、颜色等,使其不受暗/亮模式的影响?

问题描述

我的主题/颜色设置...

应用程序.xaml:

<Application.Resources>
        <Color x:Key="PageBackgroundColor">#1F1F1F</Color>
        <Style TargetType="Entry">
            <Setter Property="HorizontalOptions" Value="Fill" />
            <Setter Property="TextColor" Value="Whitesmoke" />
            <Setter Property="PlaceholderColor" Value="Red" />
            <Setter Property="HorizontalTextAlignment" Value="Center" />
        </Style>
        <Style TargetType="Label">
            <Setter Property="TextColor" Value="WhiteSmoke" />
        </Style>
        <Style TargetType="TimePicker">
            <Setter Property="TextColor" Value="WhiteSmoke" />
        </Style>
        <Style TargetType="Picker">
            <Setter Property="TextColor" Value="WhiteSmoke" />
            <Setter Property="TitleColor" Value="Red" />
        </Style>
    </Application.Resources>    

样式.xml:

<?xml version="1.0" encoding="utf-8" ?>
<resources>
  <style name="MainTheme" parent="MainTheme.Base">  
    <item name="colorAccent">#ff0000</item>    
  </style>
  <style name="MainTheme.Base" parent="Theme.AppCompat.Light">
    <item name="android:timePickerDialogTheme">@style/MyTimePickerDialogStyle</item>
  </style>
  <style name="MyTimePickerDialogStyle" parent="@style/ThemeOverlay.AppCompat.Dialog.Alert">
    <item name="colorControlActivated">#FF0000</item>
    <item name="colorAccent">#000000</item>
    <item name="android:textColorPrimary">#000000</item>
    <item name="android:textColorSecondary">#000000</item>
  </style>     
</resources>

MainPage.xaml:

<ContentPage ...
             BackgroundColor="{DynamicResource PageBackgroundColor}"
             ...>
    ...
    <Button ... TextColor="#F5F5F5" BackgroundColor="#007F00" ... />
    ...
    <Label ... BackgroundColor="#F5F5F5" ... />

无论选择何种深色/浅色模式,我的应用程序都应处于深色背景中。暗/亮模式不应影响任何颜色。Android 10 上的浅色模式没有颜色问题。使用深色模式:

为什么在地球上暗模式只影响某些颜色?我猜有一些设置可以覆盖暗模式。有什么建议么?

编辑:有没有办法停用暗模式?如何设置主题、样式、颜色等,使其不受暗/亮模式的影响?

EDIT2:截图...

灯光模式:

白色模式

黑暗模式:

黑暗模式

EDIT3:它也会影响图像。

我尝试了两张几乎相同的图像,唯一的区别是红色和绿色背景。背景的其余部分是透明的。“滑动”一词和箭头是#F5F5F5 颜色,而“A”周围的圆圈是#FFFFFF。这些图像是来自 Photoshop 的新鲜图像:

刷卡颜色

刷白

白色/深色模式下的屏幕截图:

White_mode___White_mode

如您所见,暗模式仅影响两个图像中的一个,类似于我之前提到的十六进制颜色。

标签: xamarincolorspropertiesandroid-dark-themeandroid-darkmode

解决方案


根据文档,您可以设置当前用户主题

应用程序使用的主题可以通过 Application.UserAppTheme 属性设置,该属性的类型为 OSAppTheme,无论当前运行的是哪个系统主题:

Application.Current.UserAppTheme = OSAppTheme.Dark;

您还可以在明/暗模式下设置相同的颜色:

<Label Text="This text is green in light mode, and green in dark mode."
               TextColor="{AppThemeBinding Light=Green, Dark=Green}" />

推荐阅读