更改NumberPicker的文本颜色

Oda*_*aym 43 android textcolor numberpicker

我已经查看了大部分线程,并且没有提供可行的答案.样式化NumberPicker不起作用(根据此线程:NumberPicker textColour)

将numberPicker上的style属性设置为具有颜色项的样式也没有任何效果.在numberPicker XML上设置textColor属性也不做任何事情.

我最接近的是使用numberPicker将其getChildAt()转换为EditText,然后对该EditText执行setColor(),但这只会在初始化时更改子颜色,然后每次从中选择在其上; 不是我要找的东西.

有帮助吗?谢谢

Sim*_*mon 72

此代码应该可以解决您的问题.您遇到的问题是因为在构造NumberPicker期间,它捕获EditText textColor并分配给绘制,以便它可以使用相同的颜色绘制编辑文本上方和下方的数字.

import java.lang.reflect.Field;

public static void setNumberPickerTextColor(NumberPicker numberPicker, int color)
{

    try{
        Field selectorWheelPaintField = numberPicker.getClass()
            .getDeclaredField("mSelectorWheelPaint");
        selectorWheelPaintField.setAccessible(true);
        ((Paint)selectorWheelPaintField.get(numberPicker)).setColor(color);
    }
    catch(NoSuchFieldException e){
        Log.w("setNumberPickerTextColor", e);
    }
    catch(IllegalAccessException e){
        Log.w("setNumberPickerTextColor", e);
    }
    catch(IllegalArgumentException e){
        Log.w("setNumberPickerTextColor", e);
    }

    final int count = numberPicker.getChildCount();
    for(int i = 0; i < count; i++){
        View child = numberPicker.getChildAt(i);
        if(child instanceof EditText)
            ((EditText)child).setTextColor(color);
    }
    numberPicker.invalidate();  
}
Run Code Online (Sandbox Code Playgroud)

  • 您需要做的就是覆盖主题中的`textColorPrimary`.在这种情况下,不需要调用Reflection API. (9认同)
  • 为我工作.你救了我逃离我的公寓. (4认同)
  • 我不喜欢downvote,这个答案显示出很好的努力,并且有效,但它比@Vikram的答案更糟糕的是我正在贬低,因为我认为维克拉姆的回答将更好地服务于社区. (3认同)
  • 这有效!即使使用net.simonvt.numberpicker!无论我指定什么样的风格,我都疯了,我总是以黑色文字结束!(黑色背景上的黑色文字=无用) (2认同)
  • 同意。然而,必须指出的是,这也会改变整个应用程序中的文本颜色,而不仅仅是日期时间选择器可能不适合。该问题特定于 NumberPicker。 (2认同)
  • @Simon 出于某种原因,我错过了您的回复!我猜你忘了在评论中添加“(at)Vikram”。你说:`..这也会改变你整个应用中的文本颜色..`。好吧,我们可以使用 `ContextThemeWrapper` 来创建扩展 `date/time/numberpicker` 的 `LayoutInflater`。这样,我们覆盖的 `textColorPrimary` 只影响选择器,其他一切都完好无损。但是,我确实意识到您的解决方案也不错。我在某处读到过:*反射是在运行时通过使用内省进行修改的能力*。所以,你对反射的使用是有道理的。 (2认同)

z3n*_*105 62

我试过并为我工作的解决方案是:

在styles.xml中添加:

<style name="AppTheme.Picker" parent="Theme.AppCompat.Light.NoActionBar" >
    <item name="android:textColorPrimary">@android:color/black</item>
</style>
Run Code Online (Sandbox Code Playgroud)

然后在你的布局中使用它:

  <NumberPicker
    android:id="@+id/dialogPicker"
    android:theme="@style/AppTheme.Picker"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="15dp" />
Run Code Online (Sandbox Code Playgroud)

  • @z3n105 你知道改变选择颜色的属性吗? (3认同)
  • 这是最简单也是最好的答案。+1 (2认同)
  • 这个答案会改变数字选择器所有字段的颜色,我的意思是未选定字段的颜色也会改变。但是如果我想更改刚刚选定的字段怎么办?! (2认同)

Vik*_*ram 31

不知道为什么你需要深入研究Java Reflection API.它是一个简单的造型问题.您需要覆盖的属性是:textColorPrimary.

<style name="AppTheme" parent="@android:style/Theme.Holo.Light">
    ....
    <item name="android:textColorPrimary">#ff0000</item>
</style>
Run Code Online (Sandbox Code Playgroud)

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

就是这样.

附加信息:

以下是Yoann Hercouet的深刻见解:

此解决方案不会仅更改NumberPicker上的颜色,而是一个会影响大量组件的全局更改

这是正确的,但它忽略了我所暗示的可能性.此外,global意味着应用范围的影响.通过将此主题仅应用于包含该主题的活动,可以将其限制为活动范围NumberPicker.但是,我同意,这可能仍然具有腐蚀性.

这里的想法是以某种方式注入textColorPrimary=INTENDED_COLOR将被看到的主题NumberPicker.有多种方法可以实现这一目标.这是一种方式:

在以下位置定义裸骨样式res/values/styles.xml:

<style name="NumberPickerTextColorStyle">
    <item name="android:textColorPrimary">@color/intended_color</item>
</style>
Run Code Online (Sandbox Code Playgroud)

现在,创建一个自定义NumberPicker:

public class ThemedNumberPicker extends NumberPicker {

    public ThemedNumberPicker(Context context) {
        this(context, null);
    }

    public ThemedNumberPicker(Context context, AttributeSet attrs) {
        // wrap the current context in the style we defined before
        super(new ContextThemeWrapper(context, R.style.NumberPickerTextColorStyle), attrs);
    }
}
Run Code Online (Sandbox Code Playgroud)

最后,ThemedNumberPicker在你的布局中使用:

<package.name.ThemedNumberPicker
    android:id="@+id/numberPicker"
    ....
    ....
    .... />
Run Code Online (Sandbox Code Playgroud)

我们已经成功地包含了textColorPrimary=INTENDED_COLOR对我们的应用程序的影响.

这当然只是一种选择.例如,如果您要对包含a的布局进行充气,则NumberPicker可以使用:

// In this case, the layout contains <NumberPicker... />, not <ThemedNumberPicker... />
LayoutInflater.from(new ContextThemeWrapper(context, R.style.NumberPickerTextColorStyle))
    .inflate(R.layout.number_picker_layout, ...);
Run Code Online (Sandbox Code Playgroud)

  • 一个更好的方法. (6认同)

And*_*erz 8

以下是使用TextSize和TextStyle Bold的答案的Xamarin片段

public static bool SetNumberPickerTextColorAndSize(NumberPicker numberPicker, Color color, ComplexUnitType complexUnitType, float textSize, TypefaceStyle style)
    {
        int count = numberPicker.ChildCount;
        for (int i = 0; i < count; i++)
        {
            View child = numberPicker.GetChildAt(i);
            if (child.GetType() == typeof(EditText))
            {
                try
                {
                    Field selectorWheelPaintField = numberPicker.Class
                                                                .GetDeclaredField("mSelectorWheelPaint");
                    selectorWheelPaintField.Accessible = true;

                    EditText editText = (EditText) child;
                    editText.SetTextSize(complexUnitType, textSize);
                    editText.SetTypeface(editText.Typeface, style);
                    editText.SetTextColor(color);

                    Paint paint = (Paint) selectorWheelPaintField.Get(numberPicker);
                    paint.TextSize =  TypedValue.ApplyDimension(complexUnitType, textSize, numberPicker.Resources.DisplayMetrics);
                    paint.Color = color;
                    paint.SetTypeface(editText.Typeface);

                    numberPicker.Invalidate();
                    return true;
                }
                catch (NoSuchFieldException e)
                {
                    Log.Warn("setNumberPickerTextColor", e);
                }
                catch (IllegalAccessException e)
                {
                    Log.Warn("setNumberPickerTextColor", e);
                }
                catch (IllegalArgumentException e)
                {
                    Log.Warn("setNumberPickerTextColor", e);
                }
            }
        }
        return false;
    }
Run Code Online (Sandbox Code Playgroud)

  • 非常感谢!找这个有一段时间了。如果有人在使用 Xamarin.Android,则将此代码段放在 MainActivity 或 Fragment 中。您使用 FindViewById 获取数字选择器,并将其传递给该方法。 (2认同)

Arb*_*tur 6

For me setting android:textColorPrimary in my theme did nothing, looking at the source code for the NumberPicker it decides the text color from the EditText input thus one need to set the android:editTextColor instead.

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:editTextColor">@color/dark_gray</item>
</style>
Run Code Online (Sandbox Code Playgroud)


小智 5

与其将每个文本颜色更改为您想要的颜色,不如更改所有 editText 颜色。NumberPicker 实际上有一个显示数字的子 EditText。

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
        <!-- Change edit color here. -->
        <item name="android:editTextColor">#000000</item>
</style>
Run Code Online (Sandbox Code Playgroud)

这对我有用。虽然我的按钮中有白色文字,但它们并没有改变。