完成了
我是在应用程序的styles.xml主题中进行设置(基本上在所有EditText字段上设置一个样式)
我将其放置在/values-v11/中,因此只影响高于Honeycomb的版本。
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="Theme.SelectDate" parent="@android:style/Theme.Holo.NoActionBar">
<item name="android:editTextStyle">@style/Widget.EditText.Black</item>
</style>
<style name="Widget.EditText.Black" parent="@android:style/Widget.EditText">
<item name="android:textColor">@color/black</item>
</style>
</resources>
然后在我的AndroidManifest中,针对使用DatePicker的Activity:
<activity
android:name=".ui.phone.SelectDateActivity"
android:label="Date Selection"
android:screenOrientation="portrait"
android:theme="@style/Theme.SelectDate" />
就这样!
我的思路:
我通过查看DatePicker源代码得出了这个结论:
https://github.com/android/platform_frameworks_base/blob/master/core/res/res/layout/date_picker.xml
这告诉我DatePicker使用了NumberPicker
https://github.com/android/platform_frameworks_base/blob/master/core/res/res/layout/number_picker.xml https://github.com/android/platform_frameworks_base/blob/master/core/res/res/layout/number_picker_with_selector_wheel.xml
NumberPicker使用EditText
因此,您可以为EditText设置样式
android:how to change the style of edit text?
如果在此文件中搜索“editText”,则会发现可以在一个Activity中设置所有编辑文本字段的样式!
https://github.com/android/platform_frameworks_base/blob/master/core/res/res/values/themes.xml
您可以覆盖此项:
<item name="editTextStyle">@android:style/Widget.EditText</item>
private void setNumberPickerProperties(DatePicker dp)
{
LinearLayout l = (LinearLayout)dp.getChildAt(0);
if(l!=null)
{
l = (LinearLayout)l.getChildAt(0);
if(l!=null)
{
for(int i=0;i<3;i++)
{
NumberPicker np = (NumberPicker)l.getChildAt(i);
if(np!=null)
{
EditText et = (EditText)np.getChildAt(1);
et.setTextColor(Color.BLACK);
}
}
}
}
}
你好 :) 在日期选择器小部件中有一个EditText小部件,你只需要找到它。可以使用一些创造性的编码方法,通过使用getChildAt(index)和getChildCount()等方法开始搜索datepicker小部件的子元素,然后循环遍历。
你也可以像这样做,但我不确定它是否适用于所有设备,最好循环遍历日期选择器的子元素:
DatePicker picker;
ViewGroup childpicker;
childpicker = (ViewGroup) findViewById(Resources.getSystem().getIdentifier("month" /*rest is: day, year*/, "id", "android"));
EditText textview = (EditText) picker.findViewById(Resources.getSystem().getIdentifier("timepicker_input", "id", "android"));
textview.setTextColor(Color.GREEN);
嗯,我是这样做的:
private void hackDatePickerTextColorToBlack(){
setTextColorBlack(datePicker);
}
private static void setTextColorBlack(ViewGroup v) {
int count = v.getChildCount();
for (int i = 0; i < count; i++) {
View c = v.getChildAt(i);
if(c instanceof ViewGroup){
setTextColorBlack((ViewGroup) c);
} else
if(c instanceof TextView){
((TextView) c).setTextColor(Color.BLACK);
}
}
}
这会将文本颜色更改为黑色,但递归时要小心,可能需要一些时间。
另外,当使用日期选择器时,文本会变回白色,所以很糟糕!
顺便说一下,这是DatePicker的源代码:https://github.com/android/platform_frameworks_base/blob/master/core/res/res/layout/date_picker.xml
EditTexts是NumberPickers。
setTextColorBlack()
只能起作用一次。如果我滚动日期选择器,那么所有的文本视图都会变成默认颜色,直到我点击特定的日期、月份或年份字段。 - Subrata Maandal我遇到了一个类似的问题,虽然我想要改变文本的大小,但这只是个小问题。我使用相同的步骤来拆分视图层次结构并更改字体大小。然而,一旦月份(或日期或年份)被更改,字体会恢复到原始值。对于查看来说很好,但编辑起来就不方便了。于是我采取了下一步并添加了一个更改监听器。现在当值被更改时,它会弹回到首选的字体大小:
public void setFontSize(final int size) {
LinearLayout l = (LinearLayout) mPicker.getChildAt(0);
if (l != null) {
l = (LinearLayout) l.getChildAt(0);
if (l != null) {
for (int i = 0; i < 3; i++) {
NumberPicker np = (NumberPicker) l.getChildAt(i);
for (int x = 0; x < np.getChildCount(); x++) {
View view = np.getChildAt(x);
if ((view != null) && (view instanceof TextView)) {
final TextView tv = (TextView) view;
tv.setTextSize(size);
tv.setOnEditorActionListener(new OnEditorActionListener() {
public boolean onEditorAction(TextView v,
int actionId, KeyEvent event) {
tv.setTextSize(size);
return false;
}
});
}
}
}
}
}
}