2012-01-12 100 views
14

我正在尋找一種機會來調整android honeycomb應用程序中datepicker小部件的文本顏色。我知道這個小部件固有的全球文本顏色,在我的情況下是白色的,但我需要一個黑色文本顏色作爲日期選擇器,因爲這裏的背景是淺灰色。Android HoneyComb DatePicker文本顏色

任何人都知道如何解決這個問題?

回答

2

嗨:)有一個EditText小部件在日期選擇器小部件內的某處。你只需要找到它。您可以通過使用一些創意編碼來完成此操作,並使用諸如getChildAt(index)和getChildCount()之類的方法開始通過datepicker窗口小部件的子項進行搜索,然後遍歷它。

你也可以做這樣的事情,但我不知道它會在所有設備上工作,通過datepickers兒童提供更好的循環:

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? – 2013-03-29 06:20:39

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

這改變文字顏色爲黑色,但與遞歸小心,這可能需要一些時間。

另外,當使用日期選擇器時,文本會變回白色,以至於很糟糕!

FYI這裏爲的DatePicker來源:https://github.com/android/platform_frameworks_base/blob/master/core/res/res/layout/date_picker.xml

的EditTexts是NumberPickers

27

做到了

難道它在應用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.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> 
+0

謝謝你,爲我工作。 – 2013-08-27 15:31:24

+0

很好的回答!我建議使用AppBaseTheme作爲Theme.SelectDate的父項,以便它也適用於較低的API。 – 2013-09-07 10:04:51

6

我發現這個解決方案:調試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); 
           } 
         } 
       } 
     } 
} 
0

我有一個類似的問題,但我一直在尋找改變文字大小,但是這是一個小細節。我使用相同的過程來拆開View層次結構並更改字體大小。但是,每月(或每天或每年)更改一次後,字體會變回原始值。非常適合觀看,不適合編輯。我採取了下一步並添加了一個更改監聽器。現在,當值得到改變,它會彈出回首選字體大小:

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

} 
-1

功能setTextColorBlack()只能使用一次。如果我滾動日期選擇器,那麼所有textviews都將成爲默認顏色,直到我在特定日期或月份或年份字段上選中。

+1

儘管您的_answer_不包含問號,但似乎是解決其他問題的一個問題。請不要將問題發佈爲答案。 – Bowdzone 2015-04-30 07:35:54