我试着找到类似的东西,但在上面找不到任何东西,所以...只是想知道在Android中是否有可能改变datepicker上的字体?最重要的是如何做到这一点?
下面是我的代码的一部分:
public class FirstPage extends ActionBarActivity {
DatePicker datePicker;
Typeface tf;
EditText et;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first_page);
setVar();
setFont();
}
private void setFont() {
// TODO Auto-generated method stub
String fontPath = "fonts/StarshineMF.ttf";
tf = Typeface.createFromAsset(getAssets(), fontPath);
}
private void setVar() {
// TODO Auto-generated method stub
datePicker = (DatePicker) findViewById(R.id.datePicker1);
}}
和XML代码:
<DatePicker
android:id="@+id/datePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:datePickerMode="spinner"
android:calendarViewShown="false" />发布于 2015-03-07 16:49:07
使用此代码在日期拾取器上应用字体
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);
setFontOnPicker(day);
// month
LinearLayout month = (LinearLayout) layout.getChildAt(1);
setFontOnPicker(month);
// year
LinearLayout year = (LinearLayout) layout.getChildAt(2);
setFontOnPicker(year);通过调用此方法应用字体
private void setFontOnPicker(LinearLayout ll) {
EditText et = (EditText) ll.getChildAt(1);
et.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 14);
et.setTypeface(youtTypeface);
}发布于 2019-10-30 17:11:48
我试过了,它确实对我有效,这段代码允许你覆盖datepicker的样式(字体和/或分隔符颜色):
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DatePicker datePicker = findViewById(R.id.datePicker1);
applyStyLe(datePicker);
}
private void applyStyLe(DatePicker datePickerDialog) {
Resources system = Resources.getSystem();
int dayNumberPickerId = system.getIdentifier("day","id", "android");
int monthNumberPickerId = system.getIdentifier("month", "id", "android");
int yearNumberPickerId = system.getIdentifier("year", "id", "android");
NumberPicker dayNumberPicker = datePickerDialog.findViewById(dayNumberPickerId);
NumberPicker monthNumberPicker = datePickerDialog.findViewById(monthNumberPickerId);
NumberPicker yearNumberPicker= datePickerDialog.findViewById(yearNumberPickerId);
overrideStyle(dayNumberPicker);
overrideStyle(monthNumberPicker);
overrideStyle(yearNumberPicker);
}
private void overrideStyle(NumberPicker number_picker) {
final int count = number_picker.getChildCount();
Typeface tf=Typeface.createFromAsset(getAssets(),"font.otf"); //declare your font here
for (int i = 0; i < count; i++) {
try {
//this allows you to change the divider color
Field dividerField = number_picker.getClass().getDeclaredField("mSelectionDivider");
dividerField.setAccessible(true);
ColorDrawable colorDrawable = new ColorDrawable(this.getResources().getColor(R.color.Blue));
dividerField.set(number_picker, colorDrawable);
//this allows you to change the font
Field wheelpaint_field = number_picker.getClass().getDeclaredField("mSelectorWheelPaint");
wheelpaint_field.setAccessible(true);
((Paint) wheelpaint_field.get(number_picker)).setTypeface(tf);
((EditText) number_picker.getChildAt(i)).setTypeface(tf);
number_picker.invalidate();
} catch (NoSuchFieldException e) {
Log.w("setNumberPickerTxtClr", e);
} catch (IllegalAccessException e) {
Log.w("setNumberPickerTxtClr", e);
} catch (IllegalArgumentException e) {
Log.w("setNumberPickerTxtClr", e);
}
}
}
}https://stackoverflow.com/questions/28912655
复制相似问题