如何在Android中更改DatePicker和TimePicker对话框的默认颜色?

KKC*_*KKC 24 android datepicker timepicker android-layout

有没有办法更改DatePickerTimePicker对话框的默认颜色?

这是我试过的代码

<DatePicker
                    style="@style/date_picker"
                    android:background="#6495ED"
                    android:id="@+id/DatePicker"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="5dp"
                    android:layout_marginBottom="5dp"
                    android:layout_marginLeft="5dip"
                    android:layout_marginRight="5dip" />

<TimePicker
                    android:id="@+id/TimePicker"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="5dp"
                    android:layout_marginBottom="5dp"
                    android:layout_marginLeft="5dip"
                    android:layout_marginRight="5dip" />
Run Code Online (Sandbox Code Playgroud)

只有两行改变背景颜色,我想更改日期和时间选择器的默认银色.
请帮忙.

style="@style/date_picker"
android:background="#6495ED"
Run Code Online (Sandbox Code Playgroud)

God*_*win 30

更改选择器对话框的最佳方法是向其添加自定义样式.

<style name="TimePickerTheme" parent="Theme.AppCompat.Light.Dialog">
    <item name="colorAccent">@color/color_primary</item>
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_height">wrap_content</item>
</style>

TimePickerDialog timePicker = new TimePickerDialog(mContext, R.style.TimePickerTheme, fromListener, hour, min, false);
Run Code Online (Sandbox Code Playgroud)

为我工作完美.

  • 不工作 没有错误,但是即使自定义样式中为它们设置了明显不同的值,颜色也保持不变。有什么帮助吗? (2认同)

Adi*_*tya 6

您必须创建自定义主题并将其保存在某些目录中,以便最终将此主题设置为应用程序的默认主题

首先,在值中添加一个这样的themes.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="MyAppTheme" parent="@android:style/Theme.Light.NoTitleBar">
        <!-- Any customizations for your app running on pre-3.0 devices here -->
    </style>
</resources> 
Run Code Online (Sandbox Code Playgroud)

然后,在res目录中创建一个名为"values-v11"(Android 3.0+)的目录,并放置一个这样的themes.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="MyAppTheme" parent="@android:style/Theme.Holo.Light">
        <!-- Any customizations for your app running on 3.0+ devices here -->
    </style>
</resources>
Run Code Online (Sandbox Code Playgroud)

最后,在res目录中创建一个名为"values-v14"(Android 4.0+)的目录,并创建一个themes.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="MyAppTheme" parent="@android:style/Theme.DeviceDefault.Light.NoActionBar">
        <!-- Any customizations for your app running on 4.0+ devices here -->
    </style>
</resources>
Run Code Online (Sandbox Code Playgroud)

最后在manifest.xml中

<application
        ...
        android:theme="@style/MyAppTheme">
Run Code Online (Sandbox Code Playgroud)

  • 如果需要更改的属性是公共的,那么这将是一种方法.遗憾的是,并非用于样式小部件的所有属性都是公共的.说实话,它甚至不清楚OP正在谈论什么属性. (3认同)