編輯: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 在構造函數裡就套好字體——代碼集中,後續替換字體時只改一處。
仿 iPhone 風格對話框:元件、jar 包與用法一次提供
效果圖如下: 這個對話框完全繼承並仿照 AlertDialog,只是換上自訂的外觀。由於 iPhone 的對話框大多沒有圖示,因此這裡沒有實作 setIcon。 附件包
Android 錄音功能實作:以 MediaRecorder 做按住說話
本文示範如何實作一個類似微信「按住說話」的錄音功能:手指按下開始錄音,放開停止,音檔自動存到 SD 卡。 版面配置 <LinearLayout xmlns:and
Android 網絡編程:獲取網絡上的 JSON 數據實例
要獲取網絡上的 JSON 數據,首先需要服務器端提供相應支持。本文以一個新聞列表為例,完整演示從服務器端構建 JSON 接口到 Android 端解析展示的全流程。一、創
Android 本地化入門:用 values-it 資料夾讓 App 自動說義大利語
同一支 App 可能同時被東京、法蘭克福、聖保羅的使用者打開。若介面文字寫死,「Hello world!」對誰都只會是英文。Android 的資源系統提供了一套 eleg