安卓中若要使用自定義字體,一般可以將字體的ttf文件放到assets下面,用方法
Typeface mFont = Typeface.createFromAsset(getAssets(), "Roboto-Thin.ttf");
得到字體文件對應的字體對象。
若要將ViewGroup中的所有字體更換,可使用如下方法:
public void setFont(ViewGroup group, Typeface font) {
int count = group.getChildCount();
View v;
for (int i = 0; i < count; i++) {
v = group.getChildAt(i);
if (v instanceof TextView || v instanceof EditText || v instanceof Button) {
((TextView) v).setTypeface(font);
} else if (v instanceof ViewGroup)
setFont((ViewGroup) v, font);
}
}
該方法用遞歸的手段查找ViewGroup裏的TextView,EditText,Button將其字體改變。