• Android Video Tutorials

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 資料夾。

Anroid Loading Spinner Tutorial

以下是修改後的主活動檔案 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 執行應用程式,請開啟專案的某個活動檔案,然後單擊工具欄中的執行 Eclipse Run Icon 圖示。Android Studio 將應用程式安裝到您的 AVD 並啟動它,如果您的設定和應用程式一切正常,它將顯示以下模擬器視窗:

Anroid Loading Spinner Tutorial

如您所見,出現在 AVD 上的文字沒有使用預設的 Android 字型,而是使用了您在 fonts 資料夾中指定的自定義字型。

注意 - 使用自定義字型時,您需要注意字型的尺寸和支援的字元。

廣告