Android 自定義字體
1. Android默認(rèn)方法
為每個view單獨(dú)設(shè)置Typeface.
Typeface customFont = Typeface.createFromAsset(getAssets(), "fonts/AMJT.ttf");
((TextView) findViewById(R.id.tv_hello)).setTypeface(customFont);
這種方法可以擴(kuò)展一下,將常用的Typeface保存下來,避免頻繁創(chuàng)建銷毀對象造成開銷過大引起卡頓。
1.1 新建一個工具類
public class Typefaces {
private static final Map<String, Typeface> mCache = new HashMap<>();
public static Typeface get(Context context, String name) {
synchronized (mCache) {
if (!mCache.containsKey(name)) {
Typeface t = Typeface.createFromAsset(context.getAssets(), "fonts/" + name + ".ttf");
mCache.put(name, t);
}
return mCache.get(name);
}
}
}
1.2 然后在需要的地方調(diào)用get函數(shù)
TextView view = (TextView) findViewById(R.id.tv_hello);
view.setTypeface(Typefaces.get(this, "AMJT"));
2. 擴(kuò)展TextView,Button等類
復(fù)寫其setTypeface方法
或者 通過自定義屬性來實現(xiàn)
3. 通過ViewGroup來遞歸設(shè)置字體
3.1 新建一個helper類
public class TypefaceHelper {
public static void setTypeface(final Context context, final View view, String fontName) {
if (view instanceof ViewGroup) {
for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
View v = ((ViewGroup) view).getChildAt(i);
setTypeface(context, v, fontName);
}
} else if (view instanceof TextView) {
((TextView) view).setTypeface(Typefaces.get(context, fontName));
}
}
}
這里復(fù)用了上面的Typefaces類
3.2 在activity中做如下設(shè)置(onCreate中)
TypefaceHelper.setTypeface(this, findViewById(android.R.id.content), "AMJT");
這種方式可以參考一個開源項目,自己實現(xiàn)的更完善些。https://github.com/norbsoft/android-typeface-helper
我們可以實現(xiàn)一個基類,在onCreate中調(diào)用這個方法來設(shè)置所有子view的typeface
4 全局設(shè)置
全局設(shè)置貌似并沒有什么特別好的方法。
我之前的項目中使用了一種侵入性很強(qiáng)的方法。
思路是:利用反射在程序中替換掉系統(tǒng)默認(rèn)的字體。
4.1 在application中定義如下方法
private void setDefaultFont(Context context, String fontAssetName) {
final Typeface regular = Typeface.createFromAsset(context.getAssets(),
fontAssetName);
replaceFont(regular);
}
private void replaceFont(final Typeface newTypeface) {
Map<String, Typeface> newMap = new HashMap<>();
Typeface typeface = Typeface.create("sans-serif", Typeface.NORMAL);
/* 設(shè)置默認(rèn)字體為方正蘭亭,roboto-regular原來的key是sans-serif,現(xiàn)在設(shè)置為roboto-regular
* xml或者java中如果要是用roboto-regular請使用"roboto-regular"這個key
* XML example:
* android:fontFamily="roboto-regular"
* Java example:
* Typeface typeface = Typeface.create("roboto-regular", Typeface.NORMAL);
* FIXME: 目前發(fā)現(xiàn)Button的字體改變不了,需要follow上面的代碼手動設(shè)置,XML或者JAVA
*/
newMap.put("sans-serif", newTypeface);
newMap.put("roboto-regular", typeface);
try {
final Field staticField = Typeface.class
.getDeclaredField("sSystemFontMap");
staticField.setAccessible(true);
staticField.set(null, newMap);
} catch (NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
}
}
然后在Application的onCreate中調(diào)用:
setDefaultFont(this, "fonts/FZLTHK.TTF");
之前以為這種方式會簡單,效率高點,會更方便,但是實現(xiàn)起來卻又很大的問題。
- 侵入性太強(qiáng),利用反射修改了系統(tǒng)的默認(rèn)字體(不會影響到其他應(yīng)用)。
- 對Button不起作用,沒有找到答案。對于Button還得手動去設(shè)置,并沒有減少太多工作量,還是有很多的重復(fù)代碼
5 結(jié)論
綜上,還是方法4相對更簡單每次activity create的時候會重新設(shè)置所有子view的typeface,對于全局替換的實現(xiàn)還是很方便的。
對于方法4,實現(xiàn)的比較好的一種方式可以參考https://github.com/norbsoft/android-typeface-helper。不過不建議為了字體而特意引入一個庫,可以直接實現(xiàn)一個簡單的,也更輕量級的,滿足自己項目需求就行了。