【问题标题】:Change TimePicker text color更改 TimePicker 文本颜色
【发布时间】:2014-11-18 21:09:38
【问题描述】:

我一直在尝试更改时间选择器的文本颜色。但我找不到父样式的位置。我都试过了

<style name="MyTimePicker" parent="@android:style/Widget.Holo.TimePicker">
    <item name="android:textColor">@color/text</item>
</style>

<style name="MyTimePicker" parent="@android:style/Widget.TimePicker">
    <item name="android:textColor">@color/text</item>
</style>

我的minSdkVersion 是 15。我的 targetSdkVersion 是 20。我已经重建并清理了我的项目。
我想我已经解决了关于 SO 的所有类似问题,但他们都没有真正为我提供解决方案。唯一可行的答案是使用某种库,但我不是该解决方案的忠实粉丝。到父级的路径是否与我正在使用的不同,因为我很确定我应该能够以某种方式访问​​它?

编辑
这就是主题的应用方式;

<TimePicker
    style="@style/MyTimePicker"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/timePicker"
    android:layout_below="@+id/textView"
    android:layout_centerHorizontal="true" />

请注意,这是我收到的错误(之前忘了放):

错误:检索项目的父项时出错:找不到与给定名称“@android:style/Widget.Holo.TimePicker”匹配的资源。

编辑 2 为了解决这个问题,我查看了几个问题:

  1. How can I override TimePicker to change text color - 我认为这个问题很接近答案,但我不完全确定我需要做什么?我需要将 android TimePicker 样式导入我的项目吗?
  2. https://stackoverflow.com/questions/24973586/set-textcolor-for-timepicker-in-customized-app-theme - 没有给出答案。
  3. How can i change the textcolor of my timepicker and datepicker? - 尝试了 0 票的答案,但没有成功。
  4. How to change the default color of DatePicker and TimePicker dialog in Android? - 再次以类似方式找不到 TimePicker。
  5. Android - How do I change the textColor in a TimePicker? - 再次,找不到实际的 TimePicker 父级。

这些可能是对我的问题最好的问题/答案,但没有一个能帮助我。最好能得到一个明确的答案。

【问题讨论】:

  • 第一个样式应该可以,你是如何应用那个样式的?
  • @pskink 更新答案
  • 试过this?
  • @user3249477 是的,我也试过了。它们都不起作用,因为找不到 TimePicker(我可以找到诸如操作栏之类的东西)。我可以在 android styles.xml 中找到样式,但我无法访问它们(特别是 TimePicker)
  • 我已经回答了这个问题,在这里:stackoverflow.com/questions/22962075/…

标签: android xml


【解决方案1】:

我将Paul Burke's AnswerSimon's Answer 组合在一起,成功编辑了TimePicker 的文本颜色。

这是如何完成的:

TimePicker time_picker; //Instantiated in onCreate()
Resources system;

private void set_timepicker_text_colour(){
    system = Resources.getSystem();
    int hour_numberpicker_id = system.getIdentifier("hour", "id", "android");
    int minute_numberpicker_id = system.getIdentifier("minute", "id", "android");
    int ampm_numberpicker_id = system.getIdentifier("amPm", "id", "android");

    NumberPicker hour_numberpicker = (NumberPicker) time_picker.findViewById(hour_numberpicker_id);
    NumberPicker minute_numberpicker = (NumberPicker) time_picker.findViewById(minute_numberpicker_id);
    NumberPicker ampm_numberpicker = (NumberPicker) time_picker.findViewById(ampm_numberpicker_id);

    set_numberpicker_text_colour(hour_numberpicker);
    set_numberpicker_text_colour(minute_numberpicker);
    set_numberpicker_text_colour(ampm_numberpicker);
}

private void set_numberpicker_text_colour(NumberPicker number_picker){
    final int count = number_picker.getChildCount();
    final int color = getResources().getColor(R.color.text);

    for(int i = 0; i < count; i++){
        View child = number_picker.getChildAt(i);

        try{
            Field wheelpaint_field = number_picker.getClass().getDeclaredField("mSelectorWheelPaint");
            wheelpaint_field.setAccessible(true);

            ((Paint)wheelpaint_field.get(number_picker)).setColor(color);
            ((EditText)child).setTextColor(color);
            number_picker.invalidate();
        }
        catch(NoSuchFieldException e){
            Log.w("setNumberPickerTextColor", e);
        }
        catch(IllegalAccessException e){
            Log.w("setNumberPickerTextColor", e);
        }
        catch(IllegalArgumentException e){
            Log.w("setNumberPickerTextColor", e);
        }
    }
}

请注意,这个答案现在可能已经过时了。不久前,我遇到了一些可能有问题的东西(有关更多详细信息,请参阅我的问题)。否则,您可能应该遵循 Vikram 的回答。

【讨论】:

  • java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“int android.widget.NumberPicker.getChildCount()”。 final int count = number_picker.getChildCount();
  • @voidpointer 如果您还有其他问题,请使用屏幕右上角的“提问”按钮。你所拥有的是一个普通的空指针异常。
  • @Bono 此代码在 android Lollipop 中不起作用.. hour_numberpicker ya 其他所有内容均由 null 给出,仅适用于 Android Lollipop..
  • @Bono 尝试了相同的方法来更改文本大小。但是当我们滚动改变时间的时候,大小会变成原来的大小。
  • 请注意,此答案目前可能已过时。
【解决方案2】:

不知道为什么您需要为此深入研究 Java 反射 API。这是一个简单的造型问题。您需要覆盖的属性是:textColorPrimary

<style name="AppTheme" parent="@android:style/Theme.Holo.Light">
    ....
    <item name="android:textColorPrimary">#ff0000</item>
</style>

如果您在Dialog 中使用TimePicker,请覆盖对话框主题中的android:textColorPrimary

就是这样。

【讨论】:

  • 问题是它找不到Theme.Holo.Light 父级。这就是无法更改文本颜色的原因。不幸的是,您的回答有点不同。我会尽快编辑我的问题来描述这一点。
  • @Bono 不知道为什么找不到Theme.Holo.Light。但是父母在这里无关紧要。您应该将父级保留为您已经使用的任何内容,很可能是Theme.AppCompat[.Light]
  • @Bono 更多信息:为TimePicker 提供文本颜色的attrtextColorPrimary。所以,我们可以通过覆盖上面的这个属性来改变文本颜色。但是,此属性也用作所有 TextViews 的默认文本颜色。这意味着,如果您不提供android:textColor="@color/some_color",它将使用textColorPrimary。最简单的方法是使用ContextThemeWrapper 在java 中创建TimePicker。看到这个问题已经很老了,我很感激你的评论。如果您需要使用 ContextThemeWrapper 的工作示例,请告诉我。
【解决方案3】:

一个TimePicker 实际上只是两个NumberPickers。查看Widget.NumberPicker 样式和布局,您会发现它使用了

@style/TextAppearance.Large.Inverse.NumberPickerInputText

很遗憾,TextAppearance.Large.Inverse.NumberPickerInputText 没有使用您可以在主题中设置的属性之一。所以你有两个选择:

  1. 复制必要的类以制作您自己的NumberPickerTimePicker 版本。 (您也许可以从 HoloEverywhere 之类的库中提取一些内容)

  2. 使用黑客。

如果你想走第二条路线,你可以这样做:

private int mNumberPickerInputId = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Resources system = Resources.getSystem();

    // This is the internal id of the EditText used in NumberPicker (hack)
    mNumberPickerInputId = 
        system.getIdentifier("numberpicker_input", "id", "android");

    // just used for full example, use your TimePicker
    TimePicker timePicker = new TimePicker(this);
    setContentView(timePicker);

    final int hourSpinnerId = 
        system.getIdentifier("hour", "id", "android");
    View hourSpinner = timePicker.findViewById(hourSpinnerId);
    if (hourSpinner != null) {
        setNumberPickerTextColor(hourSpinner, Color.BLUE);
    }

    final int minSpinnerId = 
        system.getIdentifier("minute", "id", "android");
    View minSpinner = timePicker.findViewById(minSpinnerId);
    if (minSpinner != null) {
        setNumberPickerTextColor(minSpinner, Color.BLUE);
    }

    final int amPmSpinnerId = 
        system.getIdentifier("amPm", "id", "android");
    View amPmSpinner = timePicker.findViewById(amPmSpinnerId);
    if (amPmSpinner != null) {
        setNumberPickerTextColor(amPmSpinner, Color.BLUE);
    }
}

private void setNumberPickerTextColor(View spinner, int color) {
    TextView input = (TextView) spinner.findViewById(mNumberPickerInputId);
    input.setTextColor(color);
}

编辑

经过进一步调查,这个 hack 并不能很好地工作。它不允许您更改 NumberPicker 高于/低于值的颜色。在使用与它交互后,颜色也会重置。看来您唯一的选择是创建自己的必要类的副本(上面的选项 #1)。

【讨论】:

  • 感谢保罗,“黑客”对我来说看起来不错。不过,我有几个问题;如何获得上方和下方的数字以更改颜色?当我旋转/更改微调器(选择不同的数字)时,它们再次默认变回黑色。如何防止这种情况发生?
  • @Bono 请查看我的更新答案。看起来选项 1 可能是正确控制它的唯一方法。 :-(
【解决方案4】:

添加到Vikram答案,设置主题为timePicker不要设置为样式

styles.xml

 <style name="timePickerOrange">
    <item name="android:textColorPrimary">@color/orange</item> <!-- color for digits -->
    <item name="android:textColor">@color/orange</item> <!-- color for colon -->
    <item name="android:colorControlNormal">@color/orange</item> <!-- color for (horizontal) delimeters -->
</style>

对于时间选择器

<TimePicker
    android:theme="@style/timePickerOrange"
    android:id="@+id/timePicker_defaultTime"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fontFamily="@font/hero_regular"
    android:timePickerMode="spinner"
    app:fontFamily="@font/hero_regular" />

【讨论】:

    【解决方案5】:

    @Vikram 是对的 就是这么简单,你可以试试这个方法

    <style name="abirStyle" parent="@android:style/Theme.Holo.Light.Dialog.NoActionBar">
        <item name="android:textColor">@color/colorPrimary</item>
        <item name="android:background">@null</item>
        <item name="android:textColorPrimary">@color/colorPrimary</item>
    </style>
    

    【讨论】:

      【解决方案6】:

      您可以尝试设置 android:textColorPrimaryInverseandroid:textColorSecondaryInverse。这应该可以在不使用反射的情况下完成这项工作。

      <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
          <item name="android:textColorPrimaryInverse">@android:color/black</item>
          <item name="android:textColorSecondaryInverse">@color/colorLightGrey</item>
      </style>
      

      【讨论】:

      • 这就是我所需要的
      猜你喜欢
      • 1970-01-01
      • 2023-03-18
      • 2015-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多