Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Android

Cách sử dụng font tùy chỉnh (custom font) trong dự án Android

Giới thiệu

Hướng dẫn này trình bày cách sử dụng font chữ tùy chỉnh (custom font) trong ứng dụng Android bằng cách đặt file font vào thư mục assets và áp dụng cho TextView.

Các bước thực hiện

Bước 1: Tạo dự án mới

Mở Android Studio, chọn File → New Project và điền thông tin cần thiết.

Bước 2: Chuẩn bị file font

  1. Tạo thư mục assets nếu chưa có: chuột phải vào main → New → Folder → Assets Folder
  2. Trong assets, tạo thư mục font
  3. Copy file font (ví dụ: AlexBrush-Regular.ttf) vào assets/font/

Bước 3: Thiết kế giao diện (res/layout/activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Hãy nở nụ cười nào!"
        android:textSize="50dp"
        android:layout_centerVertical="true"
        android:layout_marginLeft="5dp" />
</RelativeLayout>

Bước 4: Viết logic xử lý (src/MainActivity.java)

import android.graphics.Typeface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    TextView tv;
    Typeface myFont;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        tv = findViewById(R.id.textView);
        
        // Tạo Typeface từ file font trong assets
        myFont = Typeface.createFromAsset(getAssets(), "font/AlexBrush-Regular.ttf");
        
        // Áp dụng font cho TextView
        tv.setTypeface(myFont);
    }
}

Bước 5: Cấu hình Manifest (androidManifest.xml)

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="app.com.sample">
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

Cách hiện đại (API 26+ / Android 8.0+)

Từ Android Oreo, có thể sử dụng Fonts in XML - dễ dàng hơn và hiệu quả hơn:

  1. Tạo thư mục res/font/
  2. Đặt file .ttf/.otf vào đó
  3. Sử dụng trong XML: android:fontFamily="@font/alexbrush_regular"
  4. Hoặc trong code: ResourcesCompat.getFont(context, R.font.alexbrush_regular)

Lưu ý

  • Font trong assets: tải lúc runtime, tăng kích thước APK, không cache hệ thống
  • Font trong res/font: được hệ thống tối ưu, cache, hỗ trợ downloadable fonts
  • Nên dùng cách mới (res/font) cho ứng dụng hiện đại

Chạy ứng dụng

Kết nối thiết bị Android, nhấn Run trong Android Studio. TextView sẽ hiển thị với font AlexBrush viết tay đẹp mắt.

Cách sử dụng font tùy chỉnh (custom font) trong dự án Android

Cách sử dụng font tùy chỉnh (custom font) trong dự án Android