fill_parent
(phản đối) = match_parent
Đường viền của chế độ xem con mở rộng để khớp với đường viền của chế độ xem chính.
wrap_content
Biên giới của cái nhìn con quấn khít xung quanh nội dung của riêng mình.
Dưới đây là một số hình ảnh để làm cho mọi thứ rõ ràng hơn. Màu xanh lục và đỏ là TextViews
. Màu trắng là hiển thị LinearLayout
.
Mỗi View
(một TextView
, một ImageView
, một Button
, vv) cần phải thiết lập các width
và height
về quan điểm. Trong file layout xml, mà có thể trông như thế này:
android:layout_width="wrap_content"
android:layout_height="match_parent"
Bên cạnh đó thiết lập chiều rộng và chiều cao để match_parent
hoặc wrap_content
, bạn cũng có thể đặt chúng vào một số giá trị tuyệt đối:
android:layout_width="100dp"
android:layout_height="200dp"
Nói chung đó là tuy nhiên, không tốt vì nó không linh hoạt cho các thiết bị có kích thước khác nhau. Sau khi bạn đã hiểu được wrap_content
và match_parent
, điều tiếp theo cần tìm hiểu là layout_weight
.
Xem thêm
XML cho hình ảnh trên
Dọc LinearLayout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="width=wrap height=wrap"
android:background="#c5e1b0"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="width=match height=wrap"
android:background="#f6c0c0"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="width=match height=match"
android:background="#c5e1b0"/>
</LinearLayout>
ngang LinearLayout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="WrapWrap"
android:background="#c5e1b0"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="WrapMatch"
android:background="#f6c0c0"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="MatchMatch"
android:background="#c5e1b0"/>
</LinearLayout>
Note
Lời giải thích trong câu trả lời này giả định không có margin or padding. Nhưng ngay cả khi có, khái niệm cơ bản vẫn như cũ. Biên giới/khoảng cách xem chỉ được điều chỉnh bằng giá trị của lề hoặc phần đệm.
Lưu ý rằng 'fill_parent' đã được đổi tên thành' match_parent' trong API cấp 8 trở lên. – gfrigon