Android 導入外部字體的完美解決方案

第一步:在main文件夾下添加assets文件,下面再建fonts文件夾 然後引入外部字體。

第二步:建一個字體工具類。

 1 package com.xxxx.xxxx.fonts;
 2 
 3 import android.content.Context;
 4 import android.graphics.Typeface;
 5 
 6 import java.lang.reflect.Field;
 7 
 8 /**
 9  * 覆蓋字體工具類
10  * Created by leict on 2017/2/28.
11  */
12 
13 public final class FontsOverride {
14     /**
15      * 設置默認字體
16      *
17      * @param context
18      * @param staticTypefaceFieldName
19      * @param fontAssetName
20      */
21     public static void setDefaultFont(Context context, String staticTypefaceFieldName, String fontAssetName) {
22         final Typeface regular = Typeface.createFromAsset(context.getAssets(), fontAssetName);
23         replaceFont(staticTypefaceFieldName, regular);
24     }
25 
26     /**
27      * 替換字體
28      *
29      * @param staticTypefaceFieldName
30      * @param newTypeface
31      */
32     protected static void replaceFont(String staticTypefaceFieldName, final Typeface newTypeface) {
33         try {
34             final Field staticField = Typeface.class.getDeclaredField(staticTypefaceFieldName);
35             staticField.setAccessible(true);
36             staticField.set(null, newTypeface);
37         } catch (NoSuchFieldException | IllegalAccessException e) {
38             e.printStackTrace();
39         }
40     }
41 }

第三步:在全局中初始化。

 1 package com.xxxx.xxxxx.application;
 2 
 3 import android.app.Application;
 4 
 5 import com.xxxx.travel.fonts.FontsOverride;
 6 
 7 /**
 8  * 全局變量
 9  * Created by leict on 2017/2/27.
10  */
11 
12 public class BaseApplication extends Application {
13     @Override
14     public void onCreate() {
15         super.onCreate();
16         FontsOverride.setDefaultFont(this, "MONOSPACE", "fonts/simkai.ttf");
17         FontsOverride.setDefaultFont(this, "SANS", "fonts/simhei.ttf");
18         FontsOverride.setDefaultFont(this, "SERIF", "fonts/simfang.ttf");
19     }
20 }

第四步:xml代碼展示。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="世界你好"
                android:textSize="24sp"
                android:typeface="monospace" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="世界你好"
                android:textSize="24sp"
                android:typeface="sans" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="世界你好"
                android:textSize="24sp"
                android:typeface="serif" />

            <EditText
                android:layout_width="match_parent"
                android:layout_height="60dp"
                android:text="世界你好!"
                android:textSize="24sp"
                android:typeface="serif" />

            <EditText
                android:layout_width="match_parent"
                android:layout_height="60dp"
                android:text="世界你好!"
                android:textSize="24sp"
                android:typeface="sans" />

            <EditText
                android:layout_width="match_parent"
                android:layout_height="60dp"
                android:text="世界你好!"
                android:textSize="24sp"
                android:typeface="monospace" />

            <Button
                android:layout_width="match_parent"
                android:layout_height="60dp"
                android:text="提交"
                android:textSize="24sp"
                android:typeface="serif" />

            <Button
                android:layout_width="match_parent"
                android:layout_height="60dp"
                android:text="提交"
                android:textSize="24sp"
                android:typeface="sans" />

            <Button
                android:layout_width="match_parent"
                android:layout_height="60dp"
                android:text="提交"
                android:textSize="24sp"
                android:typeface="monospace" />
        </LinearLayout>
    </ScrollView>
</LinearLayout>

效果圖:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章