Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發實例

Android 自定義字體實踐:從 Typeface 加載到全局批量替換

編輯:Android 開發實例

Android 系統默認只內置三種字族:sans、serif、monospace。品牌化的 App 往往不滿足於這三個,需要換上自己的字體文件。好消息是這件事並不難,難的是如何優雅地鋪到整個界面。

一、布局中直接使用內置字族

先用一個 TableLayout 把三種默認字族擺在一起對比:

<?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" />
        <!-- 使用默認的 sans 字體 -->
        <TextView
            android:id="@+id/sans"
            android:text="Hello,World"
            android:textSize="20sp"
            android:typeface="sans" />
    </TableRow>

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

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

    <!-- 這裡沒有設定字體,我們將在代碼中設定 -->
    <TableRow>
        <TextView
            android:layout_marginRight="4px"
            android:text="custom:"
            android:textSize="20sp" />
        <TextView
            android:id="@+id/custom"
            android:text="Hello,World"
            android:textSize="20sp" />
    </TableRow>

</TableLayout>

二、加載單個自定義字體

把 ttf 文件放進 assets/fonts/ 目錄,然後用 Typeface 加載並套用到控件上:

// 得到 TextView 控件對象
TextView textView = (TextView) findViewById(R.id.custom);

// 將字體文件保存在 assets/fonts/ 目錄下,創建 Typeface 對象
Typeface typeFace = Typeface.createFromAsset(getAssets(), "fonts/DroidSansThai.ttf");

// 應用字體
textView.setTypeface(typeFace);

注意字體文件放 assets 而不是 res,路徑字符串以 assets 目錄為起點,因此寫成 fonts/xxx.ttf。如果放在 res/font/ 下(API 26 之後支持),則可以直接在 XML 裡用 android:fontFamily 引用,省去代碼。

三、給整個界面批量換字體

如果每個控件都寫一遍 setTypeface(),代碼會醜到無法維護。更好的辦法是寫一個工具類,遞歸遍歷視圖樹,遇到文本類控件就套字體,遇到容器就往下鑽:

package arui.blog.csdn.net;

import android.app.Activity;
import android.graphics.Typeface;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

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);
            }
        }
    }
}

調用時機選在 setContentView() 之後,把根容器傳進去即可:

setContentView(R.layout.main);
FontManager.changeFonts((ViewGroup) findViewById(android.R.id.content), this);

這段代碼有兩處值得改進。其一,Button 是 TextView 的子類,前面的 if 已經覆蓋了它,後面的分支永遠不會命中,寫著只是冗餘。其二,createFromAsset() 每次都重新解析字體文件,開銷不小,把它提到循環外面調用一次、複用同一個 Typeface 實例更划算——示例裡已經這麼做了,值得保持。

四、實務上的取捨

自定義字體不是沒有代價。中文字庫动辄好幾 MB,直接打包進 APK 會明顯推高安裝包體積,也會增加內存佔用;加載時如果放在主線程,低端機上可能出現首屏掉幀。常見做法是只替換標題、數字等關鍵展示場景的字體,正文仍用系統字族;或者使用字體子集化工具,只保留 App 實際用到的字符。

如果 App 有多個 Activity 都要換字體,與其在每個 Activity 裡調一遍工具類,不如自定義一層 BaseActivity 統一處理,或者乾脆繼承 TextView 寫一個 CustomTextView 在構造函數裡就套好字體——代碼集中,後續替換字體時只改一處。

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