본문 바로가기

Android (안드로이드)

Android - Custom Font 적용하기 (Fonts in XML)

To use the Fonts in XML feature on devices running Android 4.1 (API level 16) and higher, 

use the Support Library 26.


안드로이드 운영체제 4.1을 사용하는 장치에서 support library 26을 사용해야 가능합니다.


1. Font 파일 다운 받기


먼저 적용할 폰트 파일을 

다운로드 하고 시작하겠습니다.


저는 무료로 지원하는 나눔 폰트를 

다운 받아사용하고 있습니다.





2. 안드로이드 프로젝트에 Font 추가 하기


이제 안드로이드 스튜디오로 넘어가서

Project Navigation Pannel 의 res 폴더를 찾아주세요


res 아래 경로에 font 폴더가 없다면 만들어주세요.

new -> Android Resource Directory



Resource type은 font 입니다.


다운로드 받았던 폰트 파일을 res/font 에 넣어주세요.


Font 파일을 옴길 때 파일 이름을 소문자로 변경해주세요!


Font Resource File을 만들겠습니다.

New -> Font resourece File



xml 에 font-family 를 추가해

Font의 Style과 Weight를 설정하겠습니다.


<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:android="http://schemas.android.com/apk/res/android">
<font
android:fontStyle="normal"
android:fontWeight="400"
android:font="@font/nanumnarunpenr" />

<font
android:fontStyle="normal"
android:fontWeight="400"
android:font="@font/nanumnarunpenb" />
</font-family>

3. XML Layout 에서 Font 사용하기


첫 번째 TextView는

"fontFamily" 속성을 사용해 시스템 폰트가 아닌

나눔폰트를 사용했습니다.

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="@font/nanumnarunpenb"
android:text="기본 / 나눔글꼴"
android:textSize="20dp"
android:gravity="center"
android:textColor="#ffffff"
android:background="#828282"
android:padding="10dp"
android:visibility="visible"/>


두 번째 TextView는

시스템 폰트를 사용했습니다.


<TextView
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="기본 / 나눔글꼴"
android:textSize="20dp"
android:gravity="center"
android:textColor="#ffffff"
android:background="#828282"
android:padding="10dp" />

4. 실행 결과




읽어주셔서 감사합니다 :)