XAML 无法在Xamarin Project for Android中更改DatePicker小部件的背景色(Accent)

nhhxz33t  于 2023-08-01  发布在  Android
关注(0)|答案(1)|浏览(112)

bounty已结束。回答此问题可获得+100声望奖励。赏金宽限期8小时后结束。Shubham Tyagi希望引起更多关注这个问题:DatePicker颜色没有变化的原因

无法在Xamarin Forms Project for Android中更改DatePicker小部件的背景色(Accent)。TimePicker工作正常。
Pink DatePicker Issue
Correct TimePicker Color
这是我知道要更改的文件。我几乎试过了所有可能的解决办法。Xamarin项目已更新。在多个设备上进行了尝试,真实的和虚拟。
第一个月

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="launcher_background">#FFFFFF</color>
    <color name="colorPrimary">#3F51B5</color>
    <color name="colorPrimaryDark">#004193</color>
    <color name="colorAccent">#004193</color>
    <color name="colorWhite">#FFFFFF</color>
</resources>

字符串
Styles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="MainTheme" parent="MainTheme.Base">
        <item name="android:textAllCaps">false</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
    <style name="MainTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
    <style name="MainTheme.Splash" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:windowTranslucentStatus">true</item>
        <item name="android:windowTranslucentNavigation">true</item>
        <item name="android:windowDrawsSystemBarBackgrounds">true</item>
        <item name="android:windowBackground">@drawable/splash_screen</item>
        <item name="android:windowNoTitle">true</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
</resources>


自定义渲染器

public class OKCancelDatePickerRenderer : DatePickerRenderer
    {
        public OKCancelDatePickerRenderer(Context context) : base(context)
        {

        }

        protected override void OnElementChanged(ElementChangedEventArgs<DatePicker> e)
        {
            base.OnElementChanged(e);

            if (Control != null) // also tried e.NewElement etc.
            {
                Control.SetBackgroundColor(Android.Graphics.Color.Blue);
                Control.SetHighlightColor(Android.Graphics.Color.Orange);
                Control.SetHintTextColor(Android.Graphics.Color.Green);
                Control.SetLinkTextColor(Android.Graphics.Color.Yellow);
                Control.SetOutlineAmbientShadowColor(Android.Graphics.Color.Red);
                Control.SetOutlineSpotShadowColor(Android.Graphics.Color.DeepSkyBlue);
            }
        }
    }

83qze16e

83qze16e1#

请将以下代码添加到您的android项目上文件夹Resources\values的文件styles.xml中。

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

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

字符串
你可以参考我的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">#2196F3</item>
        <!-- colorPrimaryDark is used for the status bar -->
        <item name="colorPrimaryDark">#1976D2</item>
        <!-- colorAccent is used as the default value for colorControlActivated
         which is used to tint widgets -->
        <item name="colorAccent">#FF4081</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">#2196F3</item>
      </style>

</resources>

相关问题