안드로이드 fill_parent와 wrap_content, 무엇이 다를까?
안드로이드 앱을 개발하다 보면 레이아웃 XML에서 fill_parent와 wrap_content라는 두 가지 너비·높이 속성 값을 자주 마주치게 됩니다. 이 글에서는 간단한 예제 프로젝트를 직접 만들어 보면서 두 속성이 실제 화면에서 어떻게 다르게 동작하는지 단계별로 살펴보겠습니다.
핵심 개념 먼저 정리하기
fill_parent / match_parent : 부모 뷰가 허용하는 영역 전체를 꽉 채워 차지합니다. fill_parent는 구버전(API 레벨 8 이하)에서 사용되던 이름이며, 현재는 같은 의미를 가진 match_parent가 표준으로 사용됩니다.
wrap_content : 뷰가 담고 있는 콘텐츠(텍스트, 이미지 등)의 실제 크기에 맞춰 뷰의 크기가 자동으로 결정됩니다.
예제 프로젝트 만들기
1단계 — 새 프로젝트 생성
Android Studio에서 File → New Project를 선택한 뒤, 필요한 정보를 모두 입력하여 새 프로젝트를 생성합니다.
2단계 — activity_main.xml 작성
res/layout/activity_main.xml 파일에 아래 코드를 추가합니다. 첫 번째 TextView는 fill_parent를, 두 번째 TextView는 wrap_content를 사용하도록 설정했습니다.
<?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"
tools:context=".MainActivity">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="A wiki is run using wiki software, otherwise known as a wiki engine. A wiki engine is a type of content management system, but it differs from most other such systems, including blog software, in that the content is created without any defined owner or leader, and wikis have little inherent structure, allowing structure to emerge according to the needs of the users."/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="This is a Wrap text"/>
</RelativeLayout>
3단계 — MainActivity.java 작성
src/MainActivity.java 파일에 아래 코드를 추가합니다.
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단계 — 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 Studio에서 프로젝트의 액티비티 파일 중 하나를 연 뒤 툴바의 Run 아이콘을 클릭하고, 기기 목록에서 본인의 모바일 기기를 선택하면 앱이 실행됩니다. 실행된 화면은 아래와 같습니다.
실행 결과 분석
화면을 보면 두 TextView의 동작 차이가 명확하게 드러납니다.
- fill_parent (match_parent) : 첫 번째 TextView는 fill_parent로 설정되어 있어, 위키에 관한 긴 텍스트가 부모 영역인 화면 전체를 가득 채우는 것을 확인할 수 있습니다.
- wrap_content : 두 번째 TextView는 wrap_content로 설정되어 있어, "This is a Wrap text"라는 문장의 시작("This")부터 끝("text")까지의 길이만큼만 영역을 차지하고 화면 중앙에 배치됩니다.
정리 및 팁
요약하면 match_parent(fill_parent)는 사용 가능한 전체 공간을 차지하는 반면, wrap_content는 콘텐츠 크기에 딱 맞게 영역을 감싸는 방식입니다. 참고로 fill_parent는 이미 deprecated(사용 중단 권고)된 값이므로, 새 프로젝트에서는 항상 match_parent를 사용하는 것이 좋습니다. 상황에 맞게 두 속성을 적절히 조합하면 다양한 화면 크기에 유연하게 대응하는 레이아웃을 만들 수 있습니다.