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)
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)
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)
以下是使用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)
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)
这对我有用。虽然我的按钮中有白色文字,但它们并没有改变。