TimePicker, DatePicker, NumberPicker의 폰트 바꾸기
시간을 쉽게 선택할 수 있는 TimePicker,
날짜를 쉽게 선택할 수 있는 DatePicker
이것들은 NumberPicker로 이루어진 UI이다.
그래서 이 Picker들의 폰트를 바꾸기 위해서는 NumberPicker의 구조를 알아야 한다.
NumberPicker는 현재 선택된 숫자를 보여주는 TextView와
그 주위의 숫자를 스크롤하며 보여주는 휠 뷰로 구성된다.
(스타일을 Theme.Holo.Light 로 적용했을 경우)
즉, 폰트를 바꿔줄때 이 두가지를 모두 바꿔줘야 한다는 의미다.
public void setTypeface(NumberPicker picker, Typeface typeface) {
//주위의 숫자들을 보여주는 휠 뷰
try {
//클래스의 휠 Paint 객체를 꺼내 폰트를 적용
Field field = NumberPicker.class.getDeclaredField("mSelectorWheelPaint");
field.setAccessible(true);
((Paint)field.get(picker)).setTypeface(typeface);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
//선택된 숫자를 보여주는 TextView
int count = picker.getChildCount();
for(int i = 0 ; i < count ; i++) {
//자식뷰로 꺼내 폰트를 적용
View view = picker.getChildAt(i);
if (view instanceof TextView)
((TextView)view).setTypeface(typeface);
}
}
이를 TimePicker에 적용하면 이렇다.
DatePicker도 같은 방식으로 하면 된다.
public class MyTimePicker extends TimePicker {
public MyTimePicker(Context context) {
super(context);
}
public MyTimePicker(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void setTypeface(Typeface typeface) {
LinearLayout layout = (LinearLayout)getChildAt(0);
for(int i = 0 ; i < layout.getChildCount() ; i++) {
LinearLayout layout1 = (LinearLayout) layout.getChildAt(i);
//NumberPicker가 있는 자식뷰에서 적용
for(int j = 0 ; j < layout1.getChildCount() ; j++) {
View view = layout1.getChildAt(j);
if (view instanceof NumberPicker)
setTypeface((NumberPicker)view, typeface);
}
}
}
}
도움이 되셨다면~ 정성으로 빚은 저희 앱! 많은 이용 바래요:)
https://meorimal.com/index.html?tab=spaceship
https://meorimal.com/subway.html