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

วิธีเปลี่ยน Font Family ของ TextView ใน Android

ภาพรวม

ตัวอย่างนี้สาธิตวิธีการเปลี่ยนแบบอักษร (font family) ของ TextView ใน Android โดยใช้แอตทริบิวต์ android:fontFamily ซึ่งรองรับฟอนต์ระบบมากกว่า 10 แบบ

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

สร้างโปรเจกต์ Android ใหม่ใน Android Studio

ขั้นตอนที่ 2: ออกแบบ 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/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:fontFamily="cursive"
        android:layout_centerInParent="true"
        android:textSize="48sp" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/textView1"
        android:layout_marginBottom="10dp"
        android:text="Hello World"
        android:layout_centerInParent="true"
        android:fontFamily="sans-serif-black"
        android:textSize="48sp"/>

</RelativeLayout>

ฟอนต์ระบบที่รองรับใน Android

ค่า fontFamilyลักษณะ
sans-serifฟอนต์มาตรฐาน (Roboto)
sans-serif-lightบางกว่าปกติ
sans-serif-thinบางมาก
sans-serif-mediumหนาเกินปกติเล็กน้อย
sans-serif-blackหนามาก
sans-serif-condensedตัวอักษรแคบ
sans-serif-condensed-lightแคบและบาง
serifฟอนต์แบบมีเซริฟ
serif-monospaceฟอนต์แบบ monospace ที่มีเซริฟ
monospaceฟอนต์แบบ monospace (โค้ด)
cursiveฟอนต์แบบมือเขียน

ขั้นตอนที่ 3: MainActivity.java

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

ขั้นตอนที่ 4: 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>

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

ตั้งแต่ Android 8.0 (API 26) สามารถใช้ฟอนต์ไฟล์ .ttf หรือ .otf ได้โดยตรง:

  1. สร้างโฟลเดอร์ res/font/
  2. วางไฟล์ฟอนต์ไว้ (เช่น myfont.ttf)
  3. ใช้ใน XML: android:fontFamily="@font/myfont"

การทดสอบ

รันแอปจะเห็นข้อความ "Hello World!" สองบรรทัด ที่ใช้ฟอนต์ cursive และ sans-serif-black

วิธีเปลี่ยน Font Family ของ TextView ใน Android