Android HoneyComb DatePicker文本颜色



我正在寻找一种可能性来调整android蜂窝应用程序中日期选择器小部件的文本颜色。我知道小部件固有的全局文本颜色在我的情况下是白色的,但我需要日期选择器的黑色文本颜色,因为这里的背景是浅灰色。

有人知道怎么解决这个问题吗?

完成

在应用程序styles.xml中的主题中完成(基本上在所有EditText字段上设置一个样式)

我在/values-v11/中有这个,所以它只影响>HC

<?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
            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.xmlhttps://github.com/android/platform_frameworks_base/blob/master/core/res/res/layout/number_picker_with_selector_wheel.xml

NumberPicker使用EditText

因此,您可以设置EditText 的样式

android:如何更改编辑文本的样式?

如果您在该文件中搜索"editText",您会发现您可以在一个"活动"中为所有editText字段设置样式!

https://github.com/android/platform_frameworks_base/blob/master/core/res/res/values/themes.xml

您覆盖此项目:

 <item name="editTextStyle">@android:style/Widget.EditText</item>

我找到了这个解决方案:调试DatePicker对象,得到对象jerarqy。也许这不是一个优雅的解决方案,但它有效:

    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 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

EditText是NumberPickers

我也遇到了类似的问题,尽管我想更改文本大小,但这只是一个小细节。我使用了相同的过程来拆分视图层次结构并更改字体大小。但是,一旦更改了月份(或日期或年份),字体就会变回原来的值。非常适合观看,不适合编辑。我采取了下一步,并添加了一个更改侦听器。现在,当值发生更改时,它会弹回到首选的字体大小:

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

最新更新