安卓蜂巢(HoneyComb)日期选择器文本颜色

15
我正在寻找一种方法来调整Android Honeycomb应用程序中日期选择器小部件的文本颜色。我知道该小部件继承了全局文本颜色,而在我的情况下为白色,但是我需要一个黑色文本颜色来适应此处的浅灰色背景。有人知道如何解决吗?
5个回答

27

完成了

我是在应用程序的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>

很棒的答案!我建议使用AppBaseTheme作为Theme.SelectDate的父级,这样它也可以在较低的API上运行。 - Vlad Schnakovszki

6
我找到了这个解决方案:调试DatePicker对象,我得到了对象层次结构。也许这不是一个优雅的解决方案,但它有效。
    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);
                                }
                        }
                }
        }
}

2

你好 :) 在日期选择器小部件中有一个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);

希望这能有所帮助 :)

2
你在这里使用了ChildPicker吗? - Kalpesh Lakhani

1

嗯,我是这样做的:

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

0

我遇到了一个类似的问题,虽然我想要改变文本的大小,但这只是个小问题。我使用相同的步骤来拆分视图层次结构并更改字体大小。然而,一旦月份(或日期或年份)被更改,字体会恢复到原始值。对于查看来说很好,但编辑起来就不方便了。于是我采取了下一步并添加了一个更改监听器。现在当值被更改时,它会弹回到首选的字体大小:

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;
                            }
                        });
                    }
                }
            }
        }
    }

}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接