【问题标题】:Android HoneyComb DatePicker Text ColorAndroid HoneyComb DatePicker 文本颜色
【发布时间】:2012-02-08 21:12:36
【问题描述】:

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

有人知道如何解决这个问题吗?

【问题讨论】:

    标签: android coding-style datepicker textcolor


    【解决方案1】:

    完成了

    在应用程序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:

          <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 中的所有 edittext 字段上设置样式!

    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。
    【解决方案2】:

    我找到了这个解决方案:调试 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);
                                    }
                            }
                    }
            }
    }
    

    【讨论】:

      【解决方案3】:

      您好 :) 在 datepicker 小部件的某处有一个 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);
      

      我希望这会有所帮助:)

      【讨论】:

      • 你在哪里使用 childpicker?
      【解决方案4】:

      嗯,我是这样做的:

      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() 只工作一次。如果我滚动日期选择器,那么所有文本视图都将成为默认颜色,直到我在特定日期或月份或年份字段上进行选项卡。
      【解决方案5】:

      我遇到了类似的问题,虽然我希望更改文本大小,但这是一个小细节。我使用相同的过程来分离视图层次结构并更改字体大小。但是,一旦更改了一个月(或一天或一年),字体就会恢复为原始值。非常适合观看,不适合编辑。我采取了下一步并添加了一个更改侦听器。现在,当值更改时,它会弹回首选字体大小:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-02-17
        • 2017-06-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多