
- Android 基礎
- Android - 首頁
- Android - 概述
- Android - 環境搭建
- Android - 架構
- Android - 應用程式元件
- Android - Hello World 示例
- Android - 資源
- Android - 活動(Activity)
- Android - 服務(Service)
- Android - 廣播接收器(Broadcast Receiver)
- Android - 內容提供者(Content Provider)
- Android - 碎片(Fragment)
- Android - 意圖/過濾器(Intents/Filters)
- Android - 使用者介面
- Android - UI 佈局
- Android - UI 控制元件
- Android - 事件處理
- Android - 樣式和主題
- Android - 自定義元件
- Android 高階概念
- Android - 拖放
- Android - 通知
- 基於位置的服務
- Android - 傳送電子郵件
- Android - 傳送簡訊
- Android - 打電話
- 釋出 Android 應用程式
- Android 實用示例
- Android - 警報對話方塊
- Android - 動畫
- Android - 音訊捕獲
- Android - AudioManager
- Android - 自動完成
- Android - 最佳實踐
- Android - 藍牙
- Android - 相機
- Android - 剪貼簿
- Android - 自定義字型
- Android - 資料備份
- Android - 開發者工具
- Android - 模擬器
- Android - Facebook 整合
- Android - 手勢
- Android - Google Maps
- Android - 影像效果
- Android - ImageSwitcher
- Android - 內部儲存
- Android - JetPlayer
- Android - JSON 解析器
- Android - Linkedin 整合
- Android - 載入微調器
- Android - 本地化
- Android - 登入螢幕
- Android - MediaPlayer
- Android - 多點觸控
- Android - 導航
- Android - 網路連線
- Android - NFC 指南
- Android - PHP/MySQL
- Android - 進度圓圈
- Android - 進度條
- Android - 推送通知
- Android - RenderScript
- Android - RSS 閱讀器
- Android - 螢幕錄製
- Android - SDK 管理器
- Android - 感測器
- Android - 會話管理
- Android - 共享首選項
- Android - SIP 協議
- Android - 拼寫檢查器
- Android - SQLite 資料庫
- Android - 支援庫
- Android - 測試
- Android - 文字轉語音
- Android - TextureView
- Android - Twitter 整合
- Android - UI 設計
- Android - UI 模式
- Android - UI 測試
- Android - WebView 佈局
- Android - Wi-Fi
- Android - 小部件
- Android - XML 解析器
- Android 實用資源
- Android - 問答
- Android - 實用資源
- Android - 討論
Android - 自定義字型
在 Android 中,您可以為應用程式中的字串定義自己的自定義字型。您只需要從網際網路下載所需的字型,然後將其放置在 assets/fonts 資料夾中。
將字型放入 fonts 資料夾下的 assets 資料夾後,您可以透過 Typeface 類在 Java 程式碼中訪問它。首先,在程式碼中獲取 TextView 的引用。其語法如下所示:
TextView tx = (TextView)findViewById(R.id.textview1);
接下來,您需要呼叫 Typeface 類的靜態方法 createFromAsset() 從 assets 中獲取自定義字型。其語法如下所示:
Typeface custom_font = Typeface.createFromAsset(getAssets(), "fonts/font name.ttf");
最後,您需要將此自定義字型物件設定為 TextView 的 Typeface 屬性。您需要呼叫 setTypeface() 方法來執行此操作。其語法如下所示:
tx.setTypeface(custom_font);
除了這些方法之外,Typeface 類中還定義了其他方法,您可以使用它們更有效地處理字型。
序號 | 方法及描述 |
---|---|
1 |
create(String familyName, int style) 根據字型族名稱和可選樣式資訊建立 Typeface 物件 |
2 |
create(Typeface family, int style) 建立一個與指定的現有 Typeface 和指定的樣式最匹配的 Typeface 物件 |
3 |
createFromFile(String path) 從指定的字型檔案建立一個新的 Typeface |
4 |
defaultFromStyle(int style) 根據指定的樣式返回預設 Typeface 物件之一 |
5 |
getStyle() 返回 Typeface 的內在樣式屬性 |
示例
這是一個演示如何使用 Typeface 處理自定義字型的示例。它建立一個基本應用程式,顯示您在字型檔案中指定的自定義字型。
要試驗此示例,您可以在實際裝置或模擬器上執行它。
步驟 | 描述 |
---|---|
1 | 您將使用 Android Studio IDE 在 com.example.sairamkrishna.myapplication 包下建立一個 Android 應用程式。 |
2 | 從網際網路下載字型並將其放入 assets/fonts 資料夾。 |
3 | 修改 src/MainActivity.java 檔案以新增必要的程式碼。 |
4 | 修改 res/layout/activity_main 以新增相應的 XML 元件 |
5 | 執行應用程式,選擇一個正在執行的 Android 裝置,並在其上安裝應用程式並驗證結果 |
在進入程式碼部分之前,請從 Windows 資源管理器中將字型新增到 assets 資料夾。

以下是修改後的主活動檔案 MainActivity.java 的內容。
package com.example.sairamkrishna.myapplication; import android.graphics.Typeface; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.widget.TextView; public class MainActivity extends ActionBarActivity { TextView tv1,tv2; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv1=(TextView)findViewById(R.id.textView3); tv2=(TextView)findViewById(R.id.textView4); Typeface face= Typeface.createFromAsset(getAssets(), "font/font.ttf"); tv1.setTypeface(face); Typeface face1= Typeface.createFromAsset(getAssets(), "font/font1.ttf"); tv2.setTypeface(face1); } }
以下是修改後的 xml 檔案 activity_main.xml 的內容。
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Typeface" android:id="@+id/textView" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:textSize="30dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Tutorials Point" android:id="@+id/textView2" android:layout_below="@+id/textView" android:layout_centerHorizontal="true" android:textSize="35dp" android:textColor="#ff16ff01" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Tutorials Point" android:id="@+id/textView3" android:layout_centerVertical="true" android:textSize="45dp" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Tutorials Point" android:id="@+id/textView4" android:layout_below="@+id/textView3" android:layout_alignLeft="@+id/textView3" android:layout_alignStart="@+id/textView3" android:layout_marginTop="73dp" android:textSize="45dp" /> </RelativeLayout>
以下是 res/values/string.xml 的內容。
<resources> <string name="app_name">My Application</string> </resources>
以下是 AndroidManifest.xml 檔案的內容。
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.sairamkrishna.myapplication" > <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
讓我們嘗試執行我們剛剛修改的自定義字型應用程式。我假設您在進行環境設定時建立了您的 AVD。要從 Android Studio 執行應用程式,請開啟專案的某個活動檔案,然後單擊工具欄中的執行 圖示。Android Studio 將應用程式安裝到您的 AVD 並啟動它,如果您的設定和應用程式一切正常,它將顯示以下模擬器視窗:

如您所見,出現在 AVD 上的文字沒有使用預設的 Android 字型,而是使用了您在 fonts 資料夾中指定的自定義字型。
注意 - 使用自定義字型時,您需要注意字型的尺寸和支援的字元。