我想知道是否有一种方法可以遍历布局中的所有视图,并更改具有文本的所有视图(即TextView,CheckBox,EditText等)的字体。 我有一个称为setContentView()的布局,并且想知道是否有一种简单的方法来实现这一点。
我可以使用findViewById()手动执行此操作,但我宁愿有一种简单的方法来遍历它们所有。
谢谢, -克拉克-
或许这能帮你入门:
protected void changeFonts(ViewGroup root) {
Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/comicsans.ttf");
for(int i = 0; i <root.getChildCount(); i++) {
View v = root.getChildAt(i);
if(v instanceof TextView ) {
((TextView)v).setTypeface(tf);
} else if(v instanceof Button) {
((Button)v).setTypeface(tf);
} else if(v instanceof EditText) {
((EditText)v).setTypeface(tf);
} else if(v instanceof ViewGroup) {
changeFonts((ViewGroup)v);
}
}
}