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

วิธีตั้งค่า Font ให้ปุ่ม (Button) ใน Android ด้วย Kotlin

ตัวอย่างนี้สาธิตวิธี เปลี่ยน Font (Typeface) ของข้อความใน Button แบบโปรแกรมมัติ โดยใช้ Font เริ่มต้นของระบบ (Monospace, Serif, Sans-Serif)

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

Android Studio → New Project → Empty Activity

ขั้นตอนที่ 2: 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:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="50dp"
        android:text="Button Font Demo"
        android:textAlignment="center"
        android:textColor="@android:color/holo_green_dark"
        android:textSize="24sp"
        android:textStyle="bold" />

    <Button
        android:id="@+id/btnMonospace"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Click for Monospace Bold" />

    <Button
        android:id="@+id/btnSerif"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/btnMonospace"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="16dp"
        android:text="Click for Serif Bold Italic" />

</RelativeLayout>

ขั้นตอนที่ 3: โค้ดหลัก (MainActivity.kt)

import android.graphics.Typeface
import android.os.Bundle
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        title = "Button Font Demo"

        val btnMonospace = findViewById

🔤 Font Family เริ่มต้นใน Android

Constant ชื่อ Font ลักษณะ
Typeface.DEFAULT Roboto (Default) มาตรฐานของ Android
Typeface.SANS_SERIF Roboto ไม่มีขา (Sans-serif)
Typeface.SERIF Noto Serif มีขา (Serif)
Typeface.MONOSPACE Droid Sans Mono กว้างเท่ากัน (Monospace)
Typeface.CASUAL Coming Soon มือเขียน (API 16+)
Typeface.CURSIVE Dancing Script มือเขียนเรียบ (API 16+)

🎨 Style Constants

  • Typeface.NORMAL (0)
  • Typeface.BOLD (1)
  • Typeface.ITALIC (2)
  • Typeface.BOLD_ITALIC (3)

📱 ใช้ Custom Font (.ttf/.otf) - แนะนำสำหรับ Production

  1. วางไฟล์ Font ใน res/font/my_font.ttf
  2. ใช้ใน Code:
val typeface = ResourcesCompat.getFont(this, R.font.my_font)
button.typeface = typeface
  1. หรือใน XML: android:fontFamily="@font/my_font"

ขั้นตอนที่ 4: AndroidManifest.xml

Manifest มาตรฐาน

ทดสอบ

  1. รันแอป → แสดง 2 ปุ่ม ข้อความปกติ
  2. กดปุ่มแรก → Font เปลี่ยนเป็น Monospace Bold (ตัวอักษรกว้างเท่ากัน หนา)
  3. กดปุ่มที่สอง → Font เปลี่ยนเป็น Serif Bold Italic (มีขา หนา กระจาด)