Android:如何在DatePicker元素中更改所选日期的颜色? [英] Android: How to change the selected date's color in a DatePicker element?
问题描述
我是android编程的新手,我想在布局中使用DatePicker
元素.我能够成功使用它,但无法弄清楚如何更改其选定的日期颜色.我已经在该站点上阅读了许多主题,他们谈论在DatePickerDialog
中更改颜色.我已经尝试对DatePicker
元素采用类似的方法,但是无法做到.我可以使用calendarTextColor
属性更改日期的textColor
.我的布局文件代码如下:
I am new to android programming and I want to use DatePicker
element in my layout. I am able to use it successfully but I can't figure out how to change its selected date color. I have read many threads on this site and they talk about changing color in a DatePickerDialog
. I have tried to follow similar approaches for the DatePicker
element but could not do it. I can change the date's textColor
using calendarTextColor
attribute. My code for layout file is as follows:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<DatePicker
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/datePicker"
android:datePickerMode="calendar"
android:spinnersShown="false"
android:layout_gravity="center"
android:layout_marginTop="-52dp"
android:layout_marginBottom="-20dp"
style="@style/MyDatePickerStyleTheme"
/>
</LinearLayout>
这是对应的styles.xml代码:
And here is the corresponding styles.xml code:
<style name="MyDatePickerStyleTheme" parent="@android:style/Theme.Holo.Light.Dialog">
<item name="colorAccent"> #00ff00 </item>
<item name="android:datePickerStyle">@style/MyDatePickerStyle</item>
<item name="android:colorPrimary"> #00ff00 </item>
<item name="android:colorPrimaryDark"> #00ff00 </item>
<item name="android:colorAccent"> #00ff00 </item>
<item name="android:backgroundTint"> #00ff00 </item>
<!-- <item name="android:calendarTextColor"> #00ff00 </item> -->
</style>
<style name="MyDatePickerStyle">
<item name="android:calendarTextColor"> #00ff00 </item>
</style>
我想补充一些要点:
Some extra points I like to add:
-
如果将
-
android:calendarTextColor
属性放在MyDatePickerStyleTheme
中,则该属性有效,但在MydatePickerStyle
中放入时,该属性不起作用.
换句话说,后来的主题样式没有任何作用. - 在不同的android版本(Lollipop和Marshmallow)上,相同的代码提供了截然不同的布局.
- 我现在只想更改选定的日期颜色,但是如何以一种很好的方式更改日期和其他样式之间的间距.
android:calendarTextColor
attribute is working if I put it insideMyDatePickerStyleTheme
but not working when putting insideMydatePickerStyle
.
In other words the later theme style is not having any effect.- On different android versions (Lollipop and Marshmallow), same code gives very different looking layouts.
- I just want to change selected date color for now, but how to change spacing between the dates and other styles in a good manner.
任何帮助将不胜感激.
推荐答案
添加到您的styles.xml
Add to your styles.xml
<style name="MyDatePickerDialogTheme" parent="android:Theme.Material.Light.Dialog">
<item name="android:datePickerStyle">@style/MyDatePickerStyle</item>
<item name="android:colorAccent">@color/beautiful_color</item>
</style>
<style name="MyDatePickerStyle" parent="@android:style/Widget.Material.Light.DatePicker">
<item name="android:headerBackground">@color/beautiful_color</item>
<item name="android:datePickerMode">calendar</item>
</style>
然后在布局的视图中添加 android:theme 属性,而不是 style
Then in the layout where is your view, add the android:theme attribute, instead of the style
<DatePicker
android:theme="@style/MyDatePickerDialogTheme"
android:id="@+id/datePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
这篇关于Android:如何在DatePicker元素中更改所选日期的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!