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

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

ภาพรวม

ตัวอย่างนี้สาธิตการเปลี่ยน Font Style ของข้อความใน Button โดยใช้ Typeface ผ่านโค้ด Java

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

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

ขั้นตอนที่ 2: เลย์เอาต์ (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:id="@+id/rl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/btnChangeFont"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Click to change font of the Button" />

    <Button
        android:id="@+id/btnChangeFont2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_below="@id/btnChangeFont"
        android:layout_marginTop="10dp"
        android:text="Click to change font of the Button" />

</RelativeLayout>

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

import android.graphics.Typeface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    Button btnChangeFont, btnChangeFont2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnChangeFont = findViewById(R.id.btnChangeFont);
        btnChangeFont.setOnClickListener(v -> 
            btnChangeFont.setTypeface(Typeface.MONOSPACE, Typeface.BOLD)
        );

        btnChangeFont2 = findViewById(R.id.btnChangeFont2);
        btnChangeFont2.setOnClickListener(v -> 
            btnChangeFont2.setTypeface(Typeface.SERIF, Typeface.BOLD_ITALIC)
        );
    }
}

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

การทดสอบ

รันแอป กดปุ่มแรก จะเปลี่ยนเป็น Monospace Bold กดปุ่มที่สอง จะเปลี่ยนเป็น Serif Bold Italic

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

Typeface Built-in Styles

Constantคำอธิบาย
Typeface.NORMALปกติ
Typeface.BOLDตัวหนา
Typeface.ITALICตัวเอียง
Typeface.BOLD_ITALICตัวหนาเอียง
Typeface.DEFAULTDefault system font
Typeface.DEFAULT_BOLDDefault bold
Typeface.SANS_SERIFSans-serif (Roboto)
Typeface.SERIFSerif (มีขา)
Typeface.MONOSPACEMonospace (กว้างเท่ากัน)

การใช้ Custom Font (แนะนำ)

1. วางไฟล์ Font ใน res/font/ (API 26+ / Support Library 26+)

res/font/my_custom_font.ttf

2. ใช้ใน XML (API 14+ ด้วย AppCompat)

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:fontFamily="@font/my_custom_font"
    android:text="Custom Font" />

3. ใช้ในโค้ด (ResourcesCompat)

Typeface typeface = ResourcesCompat.getFont(this, R.font.my_custom_font);
button.setTypeface(typeface);

4. สร้าง Font Family XML (รองรับ Weight/Style หลายแบบ)

<!-- res/font/my_font_family.xml -->
<font-family xmlns:android="https://schemas.android.com/apk/res/android">
    <font android:fontStyle="normal" android:fontWeight="400" android:font="@font/my_font_regular" />
    <font android:fontStyle="italic" android:fontWeight="400" android:font="@font/my_font_italic" />
    <font android:fontStyle="normal" android:fontWeight="700" android:font="@font/my_font_bold" />
    <font android:fontStyle="italic" android:fontWeight="700" android:font="@font/my_font_bold_italic" />
</font-family>

คำแนะนำเพิ่มเติม

  • Downloadable Fonts - ใช้ Google Fonts Provider ดาวน์โหลดฟอนต์ lúc Run-time (ประหยัดขนาด APK)
  • Variable Fonts - ฟอนต์เดียวหลาย Weight/Style (API 28+)
  • Performance - Cache Typeface object ไว้ใช้ซ้ำ อย่าสร้างใหม่ใน onDraw()