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

Android 버튼 텍스트에 특정 글꼴 설정하는 방법 완벽 가이드

이 튜토리얼에서는 Android 앱에서 setTypeface() 메서드를 사용하여 버튼 텍스트의 글꼴과 스타일(굵게, 기울임 등)을 변경하는 방법을 단계별로 알아봅니다.

Step 1 — 새 프로젝트 생성

Android Studio를 실행한 뒤, File ⇒ New Project로 이동하여 새 프로젝트를 생성합니다. 필요한 모든 세부 정보를 입력하고 프로젝트 설정을 완료하세요.

Step 2 — 레이아웃 XML 작성

res/layout/activity_main.xml 파일에 아래 코드를 추가합니다. 화면 중앙에 두 개의 버튼을 배치합니다.

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

Step 3 — MainActivity 자바 코드 작성

src/MainActivity.java 파일에 아래 코드를 추가합니다. 각 버튼을 클릭하면 Typeface 클래스를 통해 글꼴이 변경됩니다. 첫 번째 버튼은 고정폭(MONOSPACE) 굵은 글씨로, 두 번째 버튼은 세리프(SERIF) 굵은 기울임체로 전환됩니다.

import android.graphics.Typeface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import static android.graphics.Typeface.BOLD_ITALIC;
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(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                btnChangeFont.setTypeface(Typeface.MONOSPACE,Typeface.BOLD);
            }
        });
        btnChangeFont2 = findViewById(R.id.btnChangeFont2);
        btnChangeFont2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                btnChangeFont2.setTypeface(Typeface.SERIF, BOLD_ITALIC);
            }
        });
    }
}

Step 4 — AndroidManifest.xml 확인

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>

앱 실행 및 결과 확인

이제 애플리케이션을 실행해 보겠습니다. 실제 Android 스마트폰을 컴퓨터에 연결했다고 가정합니다. Android Studio에서 프로젝트의 액티비티 파일 중 하나를 연 후, 툴바에서 Run(실행) 아이콘을 클릭하세요. 실행 옵션 목록에서 연결된 모바일 기기를 선택하면, 해당 기기에서 앱이 실행됩니다.

앱이 실행되면 기본 화면에 두 개의 버튼이 나타납니다. 첫 번째 버튼을 클릭하면 텍스트가 고정폭(MONOSPACE) 굵은 글씨로 변경되고, 두 번째 버튼을 클릭하면 세리프(SERIF) 굵은 기울임체로 변경되는 것을 확인할 수 있습니다.

Android 버튼 텍스트에 특정 글꼴 설정하는 방법 완벽 가이드

Android 버튼 텍스트에 특정 글꼴 설정하는 방법 완벽 가이드