Computer >> 컴퓨터 >  >> 프로그래밍 >> Android

Kotlin으로 안드로이드 네이티브 앱에 Font Awesome 폰트 적용하기 – 단계별 완벽 가이드

이 튜토리얼에서는 Kotlin을 사용하여 안드로이드 네이티브 애플리케이션에 Font Awesome 폰트를 적용하는 방법을 단계별로 알아봅니다. Font Awesome은 아이콘과 개성 있는 글꼴을 손쉽게 적용할 수 있어 앱의 UI를 한층 돋보이게 만들어 주는 인기 폰트 라이브러리입니다.

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/relativeLayout"
    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" />
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Have a nice day"
        android:textColor="@android:color/holo_purple"
        android:textSize="36sp"
        android:textStyle="bold" />
</RelativeLayout>

3단계: assets 폴더에 폰트 파일 추가

새로운 assets 폴더를 생성하고, 다운로드한 fontAwesome.ttf 파일을 해당 폴더에 복사해 넣습니다.

4단계: MainActivity.kt 작성

src/MainActivity.kt 파일에 아래 코드를 추가합니다.

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

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        title = "KotlinApp"
        val textView: TextView = findViewById(R.id.textView)
        val font = Typeface.createFromAsset(assets, "fontAwesome.ttf")
        textView.typeface = font
    }
}

여기서 핵심은 Typeface.createFromAsset() 메서드입니다. 이 메서드는 assets 폴더에 저장된 폰트 파일을 불러오며, 불러온 Typeface 객체를 TextView의 typeface 속성에 할당하면 해당 텍스트에 커스텀 폰트가 적용됩니다.

5단계: AndroidManifest.xml 작성

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>

앱 실행하기

이제 애플리케이션을 실행해 보겠습니다. 실제 안드로이드 기기가 컴퓨터에 연결되어 있다고 가정합니다. Android Studio에서 프로젝트의 액티비티 파일 중 하나를 연 후, 툴바의 Run Kotlin으로 안드로이드 네이티브 앱에 Font Awesome 폰트 적용하기 – 단계별 완벽 가이드 아이콘을 클릭하세요. 실행 대상 목록에서 본인의 모바일 기기를 선택하면, 기기 화면에 아래와 같이 커스텀 폰트가 적용된 결과가 표시됩니다.

Kotlin으로 안드로이드 네이티브 앱에 Font Awesome 폰트 적용하기 – 단계별 완벽 가이드