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

안드로이드 앱 전체에 기본 글꼴(Font Family)을 설정하는 방법

앱의 디자인 일관성을 높이려면 모든 화면에서 동일한 글꼴을 사용하는 것이 중요합니다. 이번 튜토리얼에서는 styles.xml 파일을 활용해 안드로이드 앱 전체에 기본 글꼴 패밀리(font family)를 한 번에 적용하는 방법을 단계별로 살펴봅니다.

1단계: 새 프로젝트 생성

Android Studio를 실행하고 File → New Project 메뉴로 이동한 뒤, 새 프로젝트 생성에 필요한 모든 정보를 입력해 프로젝트를 만듭니다.

2단계: 레이아웃 파일 작성 (activity_main.xml)

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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="16dp"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="@string/lorem"
        android:textSize="18sp" />

</RelativeLayout>

3단계: 메인 액티비티 작성 (MainActivity.java)

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

package app.tutorialspoint.com.sample;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

4단계: 테마에 기본 글꼴 지정 (styles.xml)

핵심 단계입니다. res/values/styles.xml 파일에서 android:textViewStyleandroid:buttonStyle 항목을 커스텀 스타일로 오버라이드하면, 별도의 코드 수정 없이 앱 내 모든 TextView와 Button에 동일한 글꼴이 자동으로 적용됩니다.

<resources>
    <!-- Base application theme. -->
    <style name="AppBaseTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="AppTheme" parent="AppBaseTheme">
        <item name="android:textViewStyle">@style/RobotoTextViewStyle</item>
        <item name="android:buttonStyle">@style/RobotoButtonStyle</item>
    </style>

    <!-- TextView 전용 기본 글꼴 스타일 -->
    <style name="RobotoTextViewStyle" parent="android:Widget.TextView">
        <item name="android:fontFamily">sans-serif-light</item>
    </style>

    <!-- Button 전용 기본 글꼴 스타일 -->
    <style name="RobotoButtonStyle" parent="android:Widget.Holo.Button">
        <item name="android:fontFamily">sans-serif-light</item>
    </style>

</resources>

위 예제에서는 sans-serif-light(Roboto Light 계열) 글꼴을 사용했습니다. 필요에 따라 sans-serif-medium, serif, monospace 등 다른 시스템 글꼴로 변경하거나, 직접 추가한 커스텀 폰트를 지정할 수도 있습니다.

5단계: 매니페스트에 테마 적용 (AndroidManifest.xml)

마지막으로 androidManifest.xml 파일의 application 태그에 위에서 정의한 AppTheme가 올바르게 지정되어 있는지 확인합니다.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="app.tutorialspoint.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>

실행 결과 확인하기

이제 애플리케이션을 실행해 보겠습니다. 실제 안드로이드 기기를 컴퓨터에 연결했다고 가정합니다. Android Studio에서 프로젝트의 액티비티 파일 중 하나를 연 뒤 툴바의 Run(실행) 아이콘을 클릭하세요. 목록에서 연결된 모바일 기기를 선택하면, 해당 기기에서 앱이 실행되며 아래와 같이 기본 화면에 새로 적용된 글꼴이 표시됩니다.

안드로이드 앱 전체에 기본 글꼴(Font Family)을 설정하는 방법

정리

이처럼 styles.xml에서 textViewStyle과 buttonStyle만 오버라이드하면, 각 뷰마다 개별적으로 fontFamily 속성을 설정하지 않아도 앱 전체에 일관된 글꼴을 손쉽게 적용할 수 있습니다. 유지보수 측면에서도 효율적이므로, 디자인 가이드라인이 있는 프로젝트라면 반드시 활용해 보기 바랍니다.