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

Đặt font tùy chỉnh cho Button text Android Kotlin

Hướng dẫn này minh họa cách đặt font (phông chữ) tùy chỉnh cho text của Button trong Android sử dụng Kotlin.

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: Thiết kế giao diện (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="Tutorials Point"
        android:textAlignment="center"
        android:textColor="@android:color/holo_green_dark"
        android:textSize="32sp"
        android:textStyle="bold" />

    <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_below="@id/btnChangeFont"
        android:layout_centerInParent="true"
        android:layout_marginTop="10dp"
        android:text="Click to change font of the Button" />

</RelativeLayout>

Bước 3: Viết code MainActivity.kt

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

class MainActivity : AppCompatActivity() {
    lateinit var btnChangeFont: Button
    lateinit var btnChangeFont2: Button

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        title = "KotlinApp"

        btnChangeFont = findViewById(R.id.btnChangeFont)
        btnChangeFont.setOnClickListener {
            btnChangeFont.setTypeface(Typeface.MONOSPACE, Typeface.BOLD)
        }

        btnChangeFont2 = findViewById(R.id.btnChangeFont2)
        btnChangeFont2.setOnClickListener {
            btnChangeFont2.setTypeface(Typeface.SERIF, Typeface.BOLD_ITALIC)
        }
    }
}

Bước 4: Cấu hình AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="com.example.q11">

    <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>

Chạy ứng dụng

Kết nối thiết bị, nhấn Run. Nhấn button đầu tiên → font MONOSPACE + BOLD. Nhấn button thứ hai → font SERIF + BOLD_ITALIC.

Các font hệ thống có sẵn (Typeface)

ConstantMô tả
Typeface.DEFAULTFont mặc định hệ thống (Roboto)
Typeface.SANS_SERIFFont không chân (Roboto)
Typeface.SERIFFont có chân (Noto Serif)
Typeface.MONOSPACEFont cố định chiều rộng (Droid Sans Mono)
Typeface.CASUALFont viết tay (Coming Soon)
Typeface.CURSIVEFont chữ ký (Dancing Script)

Style kết hợp

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

Sử dụng font tùy chỉnh (.ttf/.otf từ assets)

// res/font/my_font.ttf (API 26+)
btnChangeFont.setTypeface(ResourcesCompat.getFont(this, R.font.my_font))

// assets/fonts/custom_font.ttf (tất cả API)
val typeface = Typeface.createFromAsset(assets, "fonts/custom_font.ttf")
btnChangeFont.typeface = typeface

XML attribute (API 16+)

<Button
    android:fontFamily="@font/my_font"
    android:textStyle="bold|italic" />