如何设置数字选择器的中心选定值的文本大小?而且当滚动时,中心数的大小应该比其他的要大。
我尝试使用以下代码:
public static boolean setNumberPickerTextColor(NumberPicker numberPicker, int color) {
final int count = numberPicker.getChildCount();
for (int i = 0; i < count; i++) {
View child = numberPicker.getChildAt(i);
if (child instanceof EditText) {
try {
Field selectorWheelPaintField = numberPicker.getClass()
.getDeclaredField("mSelectorWheelPaint");
selectorWheelPaintField.setAccessible(true);
((Paint) selectorWheelPaintField.get(numberPicker)).setColor(color);
((EditText) child).setTextColor(color);
((EditText) child).setTextSize(48);
numberPicker.invalidate();
return true;
} catch (NoSuchFieldException | IllegalAccessException | IllegalArgumentException e) {
Log.w("setNumberPickerTextColor", e);
}
}
}
return false;
}但滚动后,文本大小将恢复正常。
发布于 2018-05-23 05:12:12
您可以使用XML和样式的强大功能来实现这一点。
首先将以下内容添加到styles.xml中
<style name="MyTheme.NumberPicker">
<item name="android:textSize">11dp</item>
</style>然后在NumberPicker中使用这种样式,如下所示:
<android.widget.NumberPicker
...
android:theme="@style/MyTheme.NumberPicker"
.../>在API +17上测试
https://stackoverflow.com/questions/31289380
复制相似问题