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

Android 自訂字型:把喜歡的 TTF 字型套用到 TextView

編輯:Android 開發實例

Android 允許你在應用程式中使用自有的字型檔,讓畫面上的文字擺脫系統預設的 Roboto,呈現品牌獨有的風格。做法很簡單:從網路下載需要的字型檔(通常是 .ttf),放進專案的 assets/fonts 目錄即可。

字型放好之後,就能在 Java 程式碼中透過 Typeface 類別取用。整個流程可以拆成三個步驟。

步驟一:取得 TextView 的參照

TextView tx = (TextView)findViewById(R.id.textview1);

步驟二:從 assets 載入字型

呼叫 Typeface 的靜態方法 createFromAsset(),第二個參數是相對於 assets 的路徑:

Typeface custom_font = Typeface.createFromAsset(getAssets(), "fonts/font name.ttf");

步驟三:把字型套用到 TextView

tx.setTypeface(custom_font);

除了上述做法,Typeface 類別還有其他實用方法,能更細緻地處理字型:

方法說明
create(String familyName, int style)依指定的字型家族名稱與樣式建立 Typeface 物件
create(Typeface family, int style)以既有的 Typeface 為基礎,套用最貼近指定樣式的變體
createFromFile(String path)從指定路徑的字型檔建立新的 Typeface
defaultFromStyle(int style)依指定樣式回傳預設的 Typeface 物件
getStyle()回傳字型本身的樣式屬性

實作範例

以下範例建立一個最簡單的應用,用自訂字型來顯示一段文字。

步驟說明
1建立 Android 專案並命名為 CustomFonts,Target SDK 選擇較新版本以使用高階 API
2從網路下載字型檔,放入 assets/fonts 目錄
3修改 src/MainActivity.java,加入必要程式碼
4修改 res/layout/activity_main.xml,加入對應元件
5修改 res/values/string.xml,補上字串資源
6執行 App,選擇真機或模擬器安裝並驗證結果

主 Activity src/com.yiibai.customfonts/MainActivity.java:

package com.example.customfonts;
import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity {
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      TextView tx = (TextView)findViewById(R.id.hello);
      Typeface custom_font = Typeface.createFromAsset(getAssets(), "fonts/Erika Type.ttf");
      tx.setTypeface(custom_font);
   }
   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
      getMenuInflater().inflate(R.menu.main, menu);
      return true;
   }
}

版面配置 res/layout/activity_main.xml:

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">
    <TextView android:id="@+id/hello"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="70dip"
        android:text="@string/hello_world" />
</LinearLayout>

字串資源 res/values/string.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <string name="app_name">CustomFonts</string>
   <string name="action_settings">Settings</string>
   <string name="hello_world">Hello</string>
</resources>

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="com.yiibai.customfonts"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" />

    <application android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity android:name="com.yiibai.customfonts.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 的預設字型,而是 assets 字型資料夾裡指定的那一款。

提醒:選用自訂字型時,務必留意字型大小是否仍具可讀性,以及該字型是否涵蓋你所需要的字符集,特別是中文、日文這類字數龐大的語言,字型檔往往動輒數 MB,會明顯增加 APK 體積。

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