編輯:關於 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)處理。
Android 中 Dialog 的用法全解析(含自訂樣式與動畫)
在 Android 開發中,Dialog 幾乎無所不在——從簡單的提示,到各種特殊效果,樣式千變萬化。每次都要翻一大堆資料,還不一定能解決問題。本文就把常用的 Dialo
Android 中利用 shape 定義控件外觀的用法
在 Android 中,我們常透過 shape 來定義控件的外觀屬性。以下整理了 shape 的幾個常用標籤與屬性。 先看下面的程式碼: <shape> &
Android WebView 啟動 Chromium 渲染引擎流程分析
Android WebView 在載入 Chromium 動態庫後,即可啟動 Chromium 渲染引擎。Chromium 渲染引擎通常由 Browser、Render
Android 入門實作:打造一個文件夾管理器(二)檔案操作與批次處理
上一篇我們已經把文件夾清單呈現出來了。這一篇要為清單子項加上事件與操作能力:點擊檔案就開啟,點擊文件夾就進入並遍歷,以此類推直到抵達最後一層。 FileOpreatio