【发布时间】:2014-11-05 19:48:44
【问题描述】:
如何在 NumberPicker 中更改字体类型。我试着这样做,但字体没有改变。任何想法? P.S: color 和 textSize 是变化的。
public class NumberPicker extends android.widget.NumberPicker {
private Context context;
private Typeface tfs;
public NumberPicker(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
tfs = Typeface.createFromAsset(context.getAssets(),"fonts/font.ttf");
}
@Override
public void addView(View child) {
super.addView(child);
updateView(child);
}
@Override
public void addView(View child, int index, android.view.ViewGroup.LayoutParams params) {
super.addView(child, index, params);
updateView(child);
}
@Override
public void addView(View child, android.view.ViewGroup.LayoutParams params) {
super.addView(child, params);
updateView(child);
}
private void updateView(View view) {
if(view instanceof EditText){
((EditText) view).setTypeface(tfs);
((EditText) view).setTextSize(25);
((EditText) view).setTextColor(Color.RED);
}
}
}
字体和路径正常工作。我将它用于我的自定义文本视图。
【问题讨论】:
-
是的,数字选择器是一个视图组,所以这就是为什么,首先你需要覆盖你自定义的扩展 android.widget.NumberPicker 的类,而不是查看嵌入到 android 并划分的 NumberPicker 的代码所有组件到组中并在代码中动态找到它,而不是从单独的视图组中获取组件。选择器->viewGroups->EditText
-
我知道很久以前有人问过这个问题,但我只是考虑为我的项目实施这个问题。
addView在构造函数之前调用,因此,当您尝试设置它时,您的字体为 null。
标签: android typeface numberpicker