Android 更改 TimePicker 文本颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26015798/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change TimePicker text color
提问by Bono
I've been trying to change the textcolor of my timepicker. But I can't find where the parent style is located. I've tried both
我一直在尝试更改时间选择器的文本颜色。但是我找不到父样式的位置。我都试过了
<style name="MyTimePicker" parent="@android:style/Widget.Holo.TimePicker">
<item name="android:textColor">@color/text</item>
</style>
and
和
<style name="MyTimePicker" parent="@android:style/Widget.TimePicker">
<item name="android:textColor">@color/text</item>
</style>
My minSdkVersion
is 15. My targetSdkVersion
is 20. I have rebuilded and cleaned my project.
I think I've been through every similar question on SO and none of them really have provided a solution for me. The only answer that might work is using some sort of library, but I'm not a big fan of that solution. Is the path to the parent something different from what I'm using, because I'm pretty sure I should be able to access it somehow?
我minSdkVersion
是 15 岁。我targetSdkVersion
是 20 岁。我已经重建并清理了我的项目。
我想我已经解决了关于 SO 的所有类似问题,但没有一个真正为我提供解决方案。唯一可行的答案是使用某种库,但我不是该解决方案的忠实粉丝。父路径是否与我使用的路径不同,因为我很确定我应该能够以某种方式访问它?
Edit
This is how the theme is applied;
编辑
这是主题的应用方式;
<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" />
On a note this is the error I receive (forgot to place it before):
请注意,这是我收到的错误(之前忘记放置了):
Error:Error retrieving parent for item: No resource found that matches the given name '@android:style/Widget.Holo.TimePicker'.
错误:检索项目的父项时出错:找不到与给定名称“@android:style/Widget.Holo.TimePicker”匹配的资源。
Edit 2A couple of the questions I've viewed to try to solve this:
编辑 2我查看过的几个问题试图解决这个问题:
- How can I override TimePicker to change text color- I think this question gets as close to an answer, but I'm not entirely sure what I need to do? Do I need to import the android TimePicker style into my project?
- https://stackoverflow.com/questions/24973586/set-textcolor-for-timepicker-in-customized-app-theme- No answer is given.
- How can i change the textcolor of my timepicker and datepicker?- Tried the 0 votes answer, but it didn't work.
- How to change the default color of DatePicker and TimePicker dialog in Android?- Again can't find the TimePicker in a similar way.
- Android - How do I change the textColor in a TimePicker?- Again, can't find the actual TimePicker parent.
- 如何覆盖 TimePicker 以更改文本颜色- 我认为这个问题接近答案,但我不完全确定我需要做什么?我需要将 android TimePicker 样式导入到我的项目中吗?
- https://stackoverflow.com/questions/24973586/set-textcolor-for-timepicker-in-customized-app-theme- 没有给出答案。
- 如何更改时间选择器和日期选择器的文本颜色?- 尝试了 0 票答案,但没有用。
- 如何更改 Android 中 DatePicker 和 TimePicker 对话框的默认颜色?- 再次无法以类似的方式找到 TimePicker。
- Android - 如何更改 TimePicker 中的 textColor?- 同样,找不到实际的 TimePicker 父级。
These are probably the best questions/answers to my problem, but none of them help me. It would be nice to get a definitive answer on this.
这些可能是对我的问题最好的问题/答案,但没有一个对我有帮助。如果能得到一个明确的答案,那就太好了。
回答by Bono
I have combined Paul Burke's Answerand Simon's Answerto succesfully edit the text colour of the TimePicker
.
我结合了Paul Burke's Answer和Simon's Answer来成功编辑TimePicker
.
Here's how it is accomplished:
这是它的实现方式:
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);
}
}
}
Please note that this answer might be outdated by now. I ran into this a while ago with something that might have been buggy (see my question for more details). Otherwise you should probably follow Vikram's answer.
请注意,这个答案现在可能已经过时了。不久前我遇到了可能有问题的东西(有关更多详细信息,请参阅我的问题)。否则,您可能应该遵循 Vikram 的回答。
回答by Vikram
Not sure why you would need to dive into Java Reflection API for this. Its a simple styling matter. The attribute that you need to override is: textColorPrimary
.
不知道为什么您需要为此深入研究 Java 反射 API。这是一个简单的造型问题。您需要覆盖的属性是:textColorPrimary
。
<style name="AppTheme" parent="@android:style/Theme.Holo.Light">
....
<item name="android:textColorPrimary">#ff0000</item>
</style>
If you're using the TimePicker
inside a Dialog
, override android:textColorPrimary
in the dialog's theme.
如果您使用TimePicker
内部 a Dialog
,则覆盖android:textColorPrimary
对话框的主题。
That's about it.
就是这样。
回答by Paul Burke
A TimePicker
is really just two NumberPicker
s. Looking into the Widget.NumberPicker
style and layout, you'll find the it uses
ATimePicker
实际上只是两个NumberPicker
s。查看Widget.NumberPicker
样式和布局,您会发现它使用的
@style/TextAppearance.Large.Inverse.NumberPickerInputText
Unfortunately, TextAppearance.Large.Inverse.NumberPickerInputText
doesn't use one of the attributes that you can set in your theme. So you have two options:
不幸的是,TextAppearance.Large.Inverse.NumberPickerInputText
不使用您可以在主题中设置的属性之一。所以你有两个选择:
Copy the necessary classes to make your own version of
NumberPicker
andTimePicker
. (You might be able to extract something from libraries like HoloEverywhere)Use hacks.
复制必要的类以制作您自己的
NumberPicker
和版本TimePicker
。(你也许可以从像HoloEverywhere这样的库中提取一些东西)使用黑客。
If you want to go the second route, you can do this:
如果你想走第二条路线,你可以这样做:
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);
}
EDIT
编辑
Upon further investigation, this hack doesn't really work well. It won't allow you to change the color of the NumberPicker
above/below values. The color also resets after the use interacts with it. It seems that your only option will be to create your own copies of the necessary classes (option #1 above).
经过进一步调查,这个 hack 并不能很好地工作。它不允许您更改NumberPicker
以上/以下值的颜色。使用后颜色也会重置。似乎您唯一的选择是创建您自己的必要类的副本(上面的选项 #1)。
回答by Muhaiminur Rahman
@Vikram is right This is so simple.You can try this way
@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>
回答by mkruglikov
You can try to set android:textColorPrimaryInverseand android:textColorSecondaryInverse. That should do the job without using Reflection.
您可以尝试设置android:textColorPrimaryInverse和android: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>
回答by Manohar Reddy
Adding to Vikramanswer , Set the theme to timePicker do not set it as style
添加到Vikram答案,将主题设置为 timePicker 不要将其设置为样式
in styles.xml
在 styles.xml
<style name="timePickerOrange">
<item name="android:textColorPrimary">@color/orange</item>
<item name="android:textColor">@color/orange</item>
</style>
and for timePicker
和时间选择器
<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" />