이 튜토리얼에서는 Android의 TextView에서 텍스트를 양쪽 정렬(justify)하는 방법을 단계별로 살펴봅니다. Android 8.0(API 레벨 26) 이상에서 도입된 justificationMode 속성을 사용하면 외부 라이브러리 없이 XML 레이아웃만으로 간단하게 양쪽 정렬을 적용할 수 있습니다.
1단계: 새 프로젝트 생성
Android Studio에서 File → New Project를 선택한 후, 새 프로젝트 생성에 필요한 모든 세부 정보를 입력하여 프로젝트를 만듭니다.
2단계: 레이아웃 파일 작성
res/layout/activity_main.xml 파일에 다음 코드를 추가합니다. 핵심은 첫 번째 TextView에 적용된 android:justificationMode="inter_word" 속성입니다. 이 속성은 단어 사이 간격을 조절하여 텍스트를 좌우 양쪽에 맞춰 정렬해 줍니다.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:app="https://schemas.android.com/apk/res-auto" xmlns:tools="https://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <TextView android:id="@+id/tvJustified" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20sp" android:padding="20sp" android:justificationMode="inter_word" android:text="The text in this TextView is justified. This feature is introduced in Android version >= 8.0. The text in this TextView is justified. This feature is introduced in Android version >= 8.0. The text in this TextView is justified. This feature is introduced in Android version >= 8.0." /> <TextView android:id="@+id/tvNotJustified" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20sp" android:padding="20sp" android:text="The text in this TextView is not justified. No justification. The text in this TextView is not justified. No justification. The text in this TextView is not justified. No justification. " /> </LinearLayout>
참고: 양쪽 정렬 효과를 확실하게 확인하려면 TextView의 너비를 match_parent로 설정하는 것이 좋습니다. wrap_content로 지정하면 텍스트 줄이 화면 끝까지 차지하지 않아 정렬 효과가 눈에 띄지 않을 수 있습니다. 또한 이 기능은 API 레벨 26(Android 8.0) 미만에서는 동작하지 않으므로, 하위 버전을 지원해야 한다면 별도의 호환 라이브러리 사용을 고려해야 합니다.
3단계: MainActivity 코드 작성
src/MainActivity.java 파일에 다음 코드를 추가합니다. 별도의 로직 없이 기본 액티비티 설정만으로 충분합니다.
package com.example.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단계: 매니페스트 파일 확인
Manifests/AndroidManifest.xml 파일에 다음 코드가 포함되어 있는지 확인합니다.
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="https://schemas.android.com/apk/res/android" package="com.example.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 아이콘을 클릭하고, 목록에서 자신의 모바일 기기를 선택하세요. 그러면 기기 화면에 앱이 실행됩니다.
실행 결과, justificationMode 속성이 적용된 TextView는 아래 이미지처럼 텍스트가 좌우 양쪽에 맞춰 정렬되어 표시되며, 속성이 적용되지 않은 TextView는 일반적인 왼쪽 정렬 상태로 나타납니다.
