【问题标题】:Android DatePicker TypefaceAndroid DatePicker 字体
【发布时间】:2012-02-09 21:51:32
【问题描述】:
我知道您可以像这样将自定义 TypeFace 应用于 Android 中的 TextView:
TextView tv = findViewById(R.id.textview01);
Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/BLACKROSE.TTF");
tv.setTypeface(tf);
有没有办法为 DatePicker 做到这一点?
【问题讨论】:
标签:
android
fonts
datepicker
typeface
【解决方案1】:
这是我的代码。我希望它对某人有用。
DatePicker datePicker = (DatePicker) findViewById(R.id.datePicker1);
LinearLayout layout1 = (LinearLayout) datePicker.getChildAt(0);
LinearLayout layout = layout1.getChildAt(0);
// day
LinearLayout day = (LinearLayout) layout.getChildAt(0);
setNumberPicker(day);
// month
LinearLayout month = (LinearLayout) layout.getChildAt(1);
setNumberPicker(month);
// year
LinearLayout year = (LinearLayout) layout.getChildAt(2);
setNumberPicker(year);
...
private void setNumberPicker(LinearLayout ll) {
((ImageButton) ll.getChildAt(0)).setBackgroundResource(R.drawable.plus_button);
((ImageButton) ll.getChildAt(2)).setBackgroundResource(R.drawable.minus_button);
EditText et = (EditText) ll.getChildAt(1);
et.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 14);
et.setTypeface(youtTypeface);
}
【解决方案2】:
查看源代码后,Datepicker 小部件包含 3 个 NumberPicker 小部件(用于日、月、年),这些小部件又包含一个 TextView。因此,您必须在 DatePicker 内的 NumberPickers 内为 TextView 设置字体。
我认为您必须同时获取 NumberPicker 和 DatePicker 的源并修改源以实现此目的,恐怕说起来容易做起来难。
【解决方案3】:
这是我的解决方案:
private void overrideFonts(View v) {
ViewGroup picker;
try {
picker = (DatePicker) v;
} catch (Exception e) {
picker = (TimePicker) v;
}
LinearLayout layout1 = (LinearLayout) picker.getChildAt(0);
if (picker instanceof TimePicker) {
if (layout1.getChildAt(1) instanceof NumberPicker) {
NumberPicker v1 = (NumberPicker) layout1.getChildAt(1);
final int count = v1.getChildCount();
for (int i = 0; i < count; i++) {
View child = v1.getChildAt(i);
try {
Field wheelpaint_field = v1.getClass().getDeclaredField("mSelectorWheelPaint");
wheelpaint_field.setAccessible(true);
((Paint) wheelpaint_field.get(v1)).setTypeface(//your font here);
((Paint) wheelpaint_field.get(v1)).setColor(getResources().getColor(R.color.colorOrange));
((EditText) child).setTypeface(// your font here);
v1.invalidate();
} catch (Exception e) {
//TODO catch.
//If java cant find field then it will catch here and app wont crash.
}
}
}
}
LinearLayout layout = (LinearLayout) layout1.getChildAt(0);
for (int j = 0; j < 3; j++) {
try {
if (layout.getChildAt(j) instanceof NumberPicker) {
NumberPicker v1 = (NumberPicker) layout.getChildAt(j);
final int count = v1.getChildCount();
for (int i = 0; i < count; i++) {
View child = v1.getChildAt(i);
try {
Field wheelpaint_field = v1.getClass().getDeclaredField("mSelectorWheelPaint");
wheelpaint_field.setAccessible(true);
((Paint) wheelpaint_field.get(v1)).setTypeface(//your font here);
((Paint) wheelpaint_field.get(v1)).setColor(getResources().getColor(R.color.colorOrange));
((EditText) child).setTypeface(//your font here);
v1.invalidate();
} catch (Exception e) {
//TODO catch.
//If java cant find field then it will catch here and app wont crash.
}
}
}
} catch (Exception e) {
//TODO catch.
//If java cant find field then it will catch here and app wont crash.
}
}
}
【解决方案4】:
如果你可以使用 DatePickerDialog,那么你可以简单地获取对话框的按钮,比如:
DatePickerDialog datePickerDialog; // GET your dialog
datePickerDialog.show();
datePickerDialog.getButton(DatePickerDialog.BUTTON_POSITIVE).setTextSize(30); // 30 is your text size
datePickerDialog.getButton(DatePickerDialog.BUTTON_NEGATIVE).setTextSize(30);
【解决方案5】:
Kotlin-Anko 相当于 SKG 的解决方案
startTimePicker = timePicker {
this.applyRecursively {
when(it) {
is NumberPicker -> {
val paintField = it.javaClass.getDeclaredField("mSelectorWheelPaint")
paintField.isAccessible = true
(paintField.get(it) as? Paint)?.typeface = //your font here
}
is TextView -> {
it.typeface = //your font here
}
}
}