在活动的所有textViews设置字体? [英] Set font for all textViews in activity?
本文介绍了在活动的所有textViews设置字体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有可能设置字体为一个活动的所有TextViews?我可以通过设置字体为一个TextView的:
Is it possible to set the font for all the TextViews in a activity? I can set the font for a single textView by using:
TextView tv=(TextView)findViewById(R.id.textView1);
Typeface face=Typeface.createFromAsset(getAssets(), "font.ttf");
tv.setTypeface(face);
不过,我想改变,而不是手动设置它为每一个TextView的所有textViews一次,所有信息将是AP preciated!
But I would like to change all the textViews at once, instead of setting it manually for every textView, any info would be appreciated!
推荐答案
解决方法1 ::通过传递父视图作为参数,只需调用这些方法。
Solution1:: Just call these method by passing parent view as argument.
private void overrideFonts(final Context context, final View v) {
try {
if (v instanceof ViewGroup) {
ViewGroup vg = (ViewGroup) v;
for (int i = 0; i < vg.getChildCount(); i++) {
View child = vg.getChildAt(i);
overrideFonts(context, child);
}
} else if (v instanceof TextView ) {
((TextView) v).setTypeface(Typeface.createFromAsset(context.getAssets(), "font.ttf"));
}
} catch (Exception e) {
}
}
溶液2 ::你也可以继承的TextView的类与您的自定义字体和使用,而不是TextView的吧。
Solution2:: you can subclass the TextView class with your custom font and use it instead of textview.
public class MyTextView extends TextView {
public MyTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MyTextView(Context context) {
super(context);
init();
}
private void init() {
if (!isInEditMode()) {
Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "font.ttf");
setTypeface(tf);
}
}
}
这篇关于在活动的所有textViews设置字体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文