Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Android

วิธีใช้ฟอนต์กำหนดเอง (Custom Font) ในโปรเจกต์ Android

การใช้ฟอนต์กำหนดเอง (Custom Font) ใน Android

ตัวอย่างนี้สาธิตวิธีนำไฟล์ฟอนต์ .ttf มาใช้กับ TextView โดยการวางไฟล์ในโฟลเดอร์ assets และโหลดผ่าน Typeface.createFromAsset()

ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่

เปิด Android Studio เลือก File → New Project และกรอกข้อมูลที่จำเป็น

ขั้นตอนที่ 2: เตรียมไฟล์ฟอนต์

  1. คลิกขวาที่โฟลเดอร์ app/src/main → New → Folder → Assets Folder
  2. สร้างโฟลเดอร์ font ภายใน assets
  3. คัดลอกไฟล์ฟอนต์ (เช่น AlexBrush-Regular.ttf) วางใน assets/font/

ขั้นตอนที่ 3: จัดทำ Layout (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="Lets put a smile on your Face!"
        android:textSize="50dp"
        android:layout_centerVertical="true"
        android:layout_marginLeft="5dp" />
</RelativeLayout>

ขั้นตอนที่ 4: เขียนโค้ดหลัก (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);
        // โหลดฟอนต์จาก assets/font/
        myFont = Typeface.createFromAsset(this.getAssets(), "font/AlexBrush-Regular.ttf");
        tv.setTypeface(myFont);
    }
}

ขั้นตอนที่ 5: กำหนดค่าใน 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>

การทดสอบ

รันแอปพลิเคชัน ข้อความจะแสดงด้วยฟอนต์ AlexBrush ที่กำหนดเอง

วิธีใช้ฟอนต์กำหนดเอง (Custom Font) ในโปรเจกต์ Android

วิธีใช้ฟอนต์กำหนดเอง (Custom Font) ในโปรเจกต์ Android

วิธีใหม่: Downloadable Fonts (API 26+)

Android 8.0+ รองรับการดาวน์โหลดฟอนต์จาก Google Fonts โดยไม่ต้องรวมไฟล์ใน APK:

<!-- res/values/fonts.xml -->
<resources>
    <array name="font_certificates">
        <item>@array/com_google_android_gms_fonts_certs</item>
    </array>
</resources>

ใช้ใน XML: android:fontFamily="@font/your_font" หรือผ่าน FontsContractCompat

XML Font Resources (API 26+)

สร้างไฟล์ res/font/myfont.xml:

<font-family xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:app="https://schemas.android.com/apk/res-auto">
    <font
        android:fontStyle="normal"
        android:fontWeight="400"
        android:font="@font/myfont_regular"
        app:font="@font/myfont_regular" />
</font-family>