如何更改 Android 中 DatePicker 和 TimePicker 对话框的默认颜色?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11077530/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change the default color of DatePicker and TimePicker dialog in Android?
提问by KKC
Is there any way to change the default color of DatePicker
and TimePicker
dialog?
有没有什么办法来改变默认的颜色DatePicker
和TimePicker
对话?
This is the code I tried
这是我试过的代码
<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" />
Below two line only changing the background color, I want to change the default silver color of both date and time picker.
Any help please.
下面两行只更改背景颜色,我想更改日期和时间选择器的默认银色。
请任何帮助。
style="@style/date_picker"
android:background="#6495ED"
回答by Godwin
The best way to change the picker dialog is by adding custom style to it.
更改选择器对话框的最佳方法是为其添加自定义样式。
<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);
Worked perfectly for me.
非常适合我。
回答by Aditya
You have to create a custom theme and save it in some directories to finally set this theme as the default one for the app
您必须创建一个自定义主题并将其保存在某些目录中才能最终将此主题设置为应用程序的默认主题
First, in values add a themes.xml like this:
首先,在 values 添加一个 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>
Then, create a directory with the name "values-v11" (Android 3.0+ ) in the res directory and put a themes.xml like this
然后,在 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>
Finally, create a directory with the name "values-v14" (Android 4.0+) in the res directory and create a themes.xml
最后,在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>
Finally in your manifest.xml
最后在你的 manifest.xml
<application
...
android:theme="@style/MyAppTheme">
回答by Julia Hexen
I think there is no way to change the grey color of the old native picker widgets easily in XML.
我认为没有办法在 XML 中轻松更改旧的本机选择器小部件的灰色。
I propose you use the library HoloEverywhereso that DatePicker and TimePicker look really nice on all Android platforms 2.1+.
我建议您使用HoloEverywhere库,以便 DatePicker 和 TimePicker 在所有 Android 平台 2.1+ 上看起来非常好。
回答by Ando Masahashi
you can change theme of your date-picker by changing theme like this.
您可以通过像这样更改主题来更改日期选择器的主题。
change items according to you.
根据您更改项目。
<style name="date_picker" parent="@android:style/Widget.DeviceDefault.DatePicker">
<item name="android:divider">@drawable/dialog_divider</item>
change items according to you. and set this theme to your data-picker style
根据您更改项目。并将此主题设置为您的数据选择器样式
style="@style/date_picker"
回答by Maryam Azhdari
For Date Picker add this codes to color.xml
对于日期选择器,将此代码添加到 color.xml
<color name="mdtp_accent_color">#070B82</color>
<color name="mdtp_accent_color_dark">#0D105E</color>
For both you can define style in style.xml
对于两者,您都可以在 style.xml 中定义样式
<style name="TimePickerTheme" parent="Theme.AppCompat.Light.Dialog">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/blue</item>
</style>
and set style in xml:
并在 xml 中设置样式:
android:theme="@style/MyAppTheme"
or programically:
或以编程方式:
TimePickerDialog timePicker = new TimePickerDialog(mContext, R.style.TimePickerTheme, fromListener, hour, min, false);
TimePickerDialog timePicker = new TimePickerDialog(mContext, R.style.TimePickerTheme, fromListener, hour, min, false);