我已经看过这方面的大多数线程,没有一个给出了有效的答案。NumberPicker的样式不工作(按照这个线程:NumberPicker textColour)
将numberPicker上的样式属性设置为具有颜色项的样式也没有任何效果。在textColor上设置numberPicker属性也不起任何作用。
与此最接近的是使用numberPicker将其getChildAt()转换为EditText,然后对该EditText执行setColor()操作,但这只会在初始化时更改子节点的颜色一次,然后每次从其子节点上选择颜色,而不是我所要寻找的颜色。
有什么帮助吗?谢谢
发布于 2014-04-09 12:14:01
这段代码应该可以解决您的问题。您所遇到的问题是,在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();
}发布于 2016-02-09 09:04:50
我尝试并为我工作过的解决方案是:
在styles.xml中添加:
<style name="AppTheme.Picker" parent="Theme.AppCompat.Light.NoActionBar" >
<item name="android:textColorPrimary">@android:color/black</item>
</style>然后在布局中像这样使用它:
<NumberPicker
android:id="@+id/dialogPicker"
android:theme="@style/AppTheme.Picker"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp" />发布于 2015-03-28 08:04:11
不确定为什么需要为此深入研究Java反射API。这是一个简单的造型问题。您需要重写的属性是:textColorPrimary。
<style name="AppTheme" parent="@android:style/Theme.Holo.Light">
....
<item name="android:textColorPrimary">#ff0000</item>
</style>如果在TimePicker中使用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>现在,创建一个自定义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);
}
}最后,在布局中使用ThemedNumberPicker:
<package.name.ThemedNumberPicker
android:id="@+id/numberPicker"
....
....
.... />我们已经成功地控制了textColorPrimary=INTENDED_COLOR对我们的应用程序的影响。
当然,这只是一个选择。例如,如果要膨胀包含NumberPicker的布局,可以使用:
// In this case, the layout contains <NumberPicker... />, not <ThemedNumberPicker... />
LayoutInflater.from(new ContextThemeWrapper(context, R.style.NumberPickerTextColorStyle))
.inflate(R.layout.number_picker_layout, ...);https://stackoverflow.com/questions/22962075
复制相似问题