Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Android

Khác biệt giữa match_parent (fill_parent) và wrap_content trong Android

Hướng dẫn này giải thích sự khác biệt giữa hai chế độ định kích thước View cơ bản nhất trong Android: match_parent (trước đây gọi là fill_parent) và wrap_content. Hiểu rõ hai khái niệm này là nền tảng để xây dựng layout hiệu quả.

Các bước thực hiện (Demo trực quan)

Bước 1: Tạo dự án mới

Mở Android Studio, chọn File → New Project và điền thông tin cần thiết.

Bước 2: Layout so sánh (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"
    tools:context=".MainActivity">

    <!-- TextView 1: match_parent (fill_parent) -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="match_parent (fill_parent): Chiếm toàn bộ không gian cha. TextView này mở rộng full màn hình.
        Một wiki chạy bằng phần mềm wiki (wiki engine). Wiki engine là một loại hệ thống quản lý nội dung,
        nhưng khác với hầu hết các hệ thống khác (như blog) vì nội dung được tạo ra không có chủ sở hữu
        hoặc người lãnh đạo xác định, và wiki có rất ít cấu trúc cố định, cho phép cấu trúc hình thành
        theo nhu cầu của người dùng." />

    <!-- TextView 2: wrap_content -->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="wrap_content: Chỉ vừa đủ bao quanh nội dung."
        android:background="#FFFF00"
        android:padding="16dp"
        android:textSize="18sp"
        android:textColor="#000000" />

</RelativeLayout>

Bước 3: MainActivity (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);
    }
}

Bước 4: Cấu hình 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>

Chạy ứng dụng

Kết nối thiết bị, nhấn Run. Màn hình sẽ hiển thị:

  • TextView xanh lớn phủ kín màn hình (match_parent)
  • TextView vàng nhỏ ở giữa, vừa khít nội dung "wrap_content: Chỉ vừa đủ bao quanh nội dung." (wrap_content)

Khác biệt giữa match_parent (fill_parent) và wrap_content trong Android

Giải thích chi tiết

match_parent (tên cũ: fill_parent)

  • Ý nghĩa: View sẽ mở rộng để bằng kích thước của ViewGroup cha (parent)
  • Tên gọi: fill_parent là tên cũ (API < 8), match_parent là tên mới (API 8+). Cùng giá trị nguyên thủy (-1)
  • Áp dụng: Cả layout_width và layout_height
  • Ví dụ: ListView, RecyclerView, ScrollView thường dùng match_parent cho cả hai chiều để lấp đầy màn hình

wrap_content

  • Ý nghĩa: View chỉ đủ lớn để chứa nội dung (content) + padding của chính nó
  • Giá trị nguyên thủy: -2
  • Hành vi: Tự động co giãn theo nội dung (text, image, child views)
  • Ví dụ: Button, TextView, ImageView thường dùng wrap_content cho ít nhất một chiều

So sánh trực quan

Đặc điểm match_parent wrap_content
Kích thước Bằng parent Bằng nội dung + padding
Thay đổi khi nội dung thay đổi Không Có
Phụ thuộc vào parent Có Không (trừ khi parent quá nhỏ)
Thường dùng cho Container, ListView, Fragment container, Background Button, TextView, ImageView, Item layout

Kết hợp thực tế

Thường kết hợp cả hai trong cùng một layout:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <!-- Header: rộng full, cao vừa đủ -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Tiêu đề" />

    <!-- Content: rộng full, cao full phần còn lại -->
    <androidx.recyclerview.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

    <!-- Footer: rộng full, cao vừa đủ -->
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Xác nhận" />

</LinearLayout>

Lưu ý quan trọng

  • Trong ConstraintLayout, match_parent không được hỗ trợ trực tiếp cho width/height. Dùng 0dp (MATCH_CONSTRAINT) kết hợp constraints (left/right/top/bottom)
  • wrap_content trong ConstraintLayout hoạt động bình thường
  • Tránh lồng quá nhiều layout dùng match_parent - ảnh hưởng hiệu năng đo lường (measure pass)