Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 自訂字體實作:從單一 TextView 到整個畫面套用

編輯:關於 Android 程式設計

Android 系統預設只內建三種字型:sans、serif、monospace。品牌需要特定字型時,就得自行引入字型檔。

一、使用系統內建字型

在版面檔中直接用 android:typeface 指定即可:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TableRow>
        <TextView
            android:layout_marginRight="4px"
            android:text="sans:"
            android:textSize="20sp" >
        </TextView>
        <!-- 使用預設的 sans 字型 -->
        <TextView
            android:id="@+id/sans"
            android:text="Hello,World"
            android:textSize="20sp"
            android:typeface="sans" >
        </TextView>
    </TableRow>

    <TableRow>
        <TextView
            android:layout_marginRight="4px"
            android:text="serif:"
            android:textSize="20sp" >
        </TextView>
        <!-- 使用預設的 serif 字型 -->
        <TextView
            android:id="@+id/serif"
            android:text="Hello,World"
            android:textSize="20sp"
            android:typeface="serif" >
        </TextView>
    </TableRow>

    <TableRow>
        <TextView
            android:layout_marginRight="4px"
            android:text="monospace:"
            android:textSize="20sp" >
        </TextView>
        <!-- 使用預設的 monospace 字型 -->
        <TextView
            android:id="@+id/monospace"
            android:text="Hello,World"
            android:textSize="20sp"
            android:typeface="monospace" >
        </TextView>
    </TableRow>

    <!-- 這一列不指定字型,稍後在 Java 中設定 -->
    <TableRow>
        <TextView
            android:layout_marginRight="4px"
            android:text="custom:"
            android:textSize="20sp" >
        </TextView>
        <TextView
            android:id="@+id/custom"
            android:text="Hello,World"
            android:textSize="20sp" >
        </TextView>
    </TableRow>
</TableLayout>

二、套用自訂字型到單一控件

把字型檔放進 assets/fonts/,再用 Typeface 載入:

// 取得 TextView 物件
TextView textView = (TextView) findViewById(R.id.custom);
// 字型檔放在 assets/fonts/ 目錄下
Typeface typeFace = Typeface.createFromAsset(getAssets(), "fonts/DroidSansThai.ttf");
// 套用字型
textView.setTypeface(typeFace);

三、整個畫面統一換字型

逐一設定太繁瑣,可以寫一個遞迴的工具類,遍歷視圖樹並依型別套用:

public class FontManager {

    public static void changeFonts(ViewGroup root, Activity act) {
        Typeface tf = Typeface.createFromAsset(act.getAssets(), "fonts/xxx.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, act);
            }
        }
    }
}

由於 Button 與 EditText 都繼承自 TextView,第一個 instanceof TextView 分支其實已經涵蓋了兩者,後面的分支可以省掉。實務上更乾淨的寫法是只判斷一次:

public static void changeFonts(ViewGroup root, Typeface tf) {
    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 ViewGroup) {
            changeFonts((ViewGroup) v, tf);
        }
    }
}

四、效能與實務提醒

不要重複建立 Typeface。Typeface.createFromAsset() 會解析整份字型檔,成本不低。如果在 getView() 或 onCreate() 裡反覆呼叫,很容易造成清單滑動卡頓。建議用靜態變數或單例快取,只載入一次:

private static Typeface sCustomTypeface;

public static Typeface get(Context c) {
    if (sCustomTypeface == null) {
        sCustomTypeface = Typeface.createFromAsset(
                c.getApplicationContext().getAssets(), "fonts/xxx.ttf");
    }
    return sCustomTypeface;
}

字型檔放 res/font 更好。新版 Android 支援把字型放進 res/font/,接著就能在版面中直接寫 android:fontFamily="@font/xxx",不必跑 Java 程式碼,也能享有資源系統的快取與相容性處理。

別忘了字型授權。商用字型多數需要授權,若要隨 App 打包,務必確認使用範圍;開源字型(如 Noto、Roboto)則是安全的选择。

中文字型檔案很大。一套完整 CJK 字型動輒數 MB 到十幾 MB,會明顯推高 APK 體積,也會拖慢首次載入。若只需要少量字重,建議先做子集化(subset)處理。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved