【问题标题】:How to set colors for DatePicker dialog in Xamarin?如何在 Xamarin 中为 DatePicker 对话框设置颜色?
【发布时间】:2016-10-06 17:30:50
【问题描述】:

我试图弄清楚如何更改当您点击日期或时间选择器时显示的对话框的背景和文本颜色。这是在 Xamarin 表单项目中,特别是在 android 应用程序中。这些属性没有暴露在 DatePicker 控件或我能找到的渲染器中的任何地方......目前它显示为这种亮粉色......谢谢!

【问题讨论】:

    标签: xamarin datepicker xamarin.android xamarin.forms


    【解决方案1】:

    在您的 style.xml 中添加项目:“android:datePickerDialogTheme”

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

    完整的style.xml

        <?xml version="1.0" encoding="UTF-8"?>
    <resources>
        <style name="MyTheme" parent="MyTheme.Base">
        </style>
        <!-- Base theme applied no matter what API -->
        <style name="MyTheme.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">#2196F3</item>
            <!-- colorAccent is used as the default value for colorControlActivated
             which is used to tint widgets -->
            <item name="colorAccent">#2196F3</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>
        <style name="MyDatePickerStyle" parent="@android:style/Widget.Material.Light.DatePicker">
            <item name="android:headerBackground">#2196F3</item>
        </style>
        <style name="MyDatePickerDialogTheme" parent="android:Theme.Material.Light.Dialog">
            <item name="android:datePickerStyle">@style/MyDatePickerStyle</item>
        </style>
    </resources>
    

    【讨论】:

    • 感谢您的回复!再看一遍,我在 app.xaml 文件中定义了 ios 和 android 的样式,然后在 xamarin 表单控件上,我将样式定义为我想要的 app.xaml 样式中的样式。只是将上面的 xml 放到我的 style.xml 文件中并没有改变颜色,有没有办法可以同时使用 app.xaml 文件和 style.xml?如何告诉控件使用适当的样式?
    猜你喜欢
    • 1970-01-01
    • 2011-09-10
    • 1970-01-01
    • 2023-02-08
    • 2015-04-28
    • 2013-09-13
    • 2021-10-01
    • 2016-09-08
    • 1970-01-01
    相关资源
    最近更新 更多