LinearLayout
là một nhóm khung hiển thị căn chỉnh tất cả phần tử con theo một hướng, chiều dọc hoặc
theo chiều ngang. Bạn có thể chỉ định hướng bố cục bằng thuộc tính
android:orientation
.
Hình 1.LinearLayout có 3 theo chiều ngang
trẻ em.
Tất cả phần tử con của LinearLayout được xếp chồng lần lượt nhau,
vì vậy, danh sách dọc chỉ có một phần tử con trên mỗi hàng, bất kể độ rộng của các phần tử đó. Đáp
danh sách ngang chỉ cao một hàng và đó là chiều cao của phần tử con cao nhất,
cộng với khoảng đệm. LinearLayout tuân theo lề giữa
phần tử con và trọng lực – bên phải, chính giữa hoặc bên trái
căn chỉnh—của từng phần tử con.
Trọng số bố cục
LinearLayout cũng hỗ trợ chỉ định trọng số cho
những con cá nhân có
android:layout_weight
. Thuộc tính này gán một "mức độ quan trọng" cho một lượt xem theo
nó chiếm bao nhiêu không gian trên màn hình. Giá trị trọng số lớn hơn cho phép quảng cáo mở rộng
để lấp đầy không gian còn lại trong khung hiển thị mẹ. Chế độ xem con có thể chỉ định trọng số
giá trị và mọi không gian còn lại trong nhóm khung hiển thị sẽ được gán cho các thành phần con
tương ứng, dựa trên trọng số đã khai báo. Trọng số mặc định là 0.
Phân bổ đồng đều
Để tạo bố cục tuyến tính, trong đó mỗi thành phần con sử dụng cùng một lượng không gian
trên màn hình, hãy đặt
android:layout_height
của mỗi thành phần hiển thị thành "0dp" cho bố cục dọc hoặc
android:layout_width
của mỗi thành phần hiển thị thành "0dp" cho bố cục ngang. Sau đó, đặt
android:layout_weight của mỗi chế độ xem đến "1".
Phân bổ không đồng đều
Bạn cũng có thể tạo bố cục tuyến tính trong đó các phần tử con sử dụng các kiểu bố cục khác nhau
một lượng không gian trên màn hình. Hãy xem các ví dụ sau đây:
Giả sử bạn có ba trường văn bản: hai trường có giá trị trọng số là 1 và một trường
thứ ba có giá trị trọng số mặc định là 0. Trường văn bản thứ ba có
giá trị trọng số bằng 0, chỉ chiếm diện tích cần thiết cho nội dung của hàm. Khác
hai trường văn bản, với giá trị trọng số là 1, hãy mở rộng đều để lấp đầy
khoảng trống còn lại sau khi nội dung của cả ba trường được đo.
Nếu bạn có 3 trường văn bản, trong đó 2 trường có giá trị trọng số là 1
và phần tử thứ ba có trọng số là 2, thì phần còn lại sau phần tử
nội dung của cả ba trường được phân bổ như sau: 1/2 đến 2
trường có giá trị trọng số là 2 và một nửa được chia đều cho
có giá trị trọng số là 1.
Hình và đoạn mã sau đây cho thấy cách hoạt động của trọng số bố cục trong một
"gửi tin nhắn" của bạn. Trường Tới, dòng Chủ đề và nút Gửi chỉ chiếm chiều cao mà các phần tử đó cần. Vùng thông báo
chiếm phần còn lại trong chiều cao của hoạt động.
Hình 2. Ba trường văn bản và một nút theo hướng dọc
LinearLayout.
Để biết thông tin chi tiết về các thuộc tính có sẵn cho mỗi chế độ xem con của một
LinearLayout, xem
LinearLayout.LayoutParams
Nội dung và mã mẫu trên trang này phải tuân thủ các giấy phép như mô tả trong phần Giấy phép nội dung. Java và OpenJDK là nhãn hiệu hoặc nhãn hiệu đã đăng ký của Oracle và/hoặc đơn vị liên kết của Oracle.
Cập nhật lần gần đây nhất: 2025-07-27 UTC.
[[["Dễ hiểu","easyToUnderstand","thumb-up"],["Giúp tôi giải quyết được vấn đề","solvedMyProblem","thumb-up"],["Khác","otherUp","thumb-up"]],[["Thiếu thông tin tôi cần","missingTheInformationINeed","thumb-down"],["Quá phức tạp/quá nhiều bước","tooComplicatedTooManySteps","thumb-down"],["Đã lỗi thời","outOfDate","thumb-down"],["Vấn đề về bản dịch","translationIssue","thumb-down"],["Vấn đề về mẫu/mã","samplesCodeIssue","thumb-down"],["Khác","otherDown","thumb-down"]],["Cập nhật lần gần đây nhất: 2025-07-27 UTC."],[],[],null,["# Create a linear layout\n\nTry the Compose way \nJetpack Compose is the recommended UI toolkit for Android. Learn how to work with layouts in Compose. \n[Column or Row →](/jetpack/compose/layouts/basics) \n\n[LinearLayout](/reference/android/widget/LinearLayout)\nis a view group that aligns all children in a single direction, vertically or\nhorizontally. You can specify the layout direction with the\n[`android:orientation`](/reference/android/widget/LinearLayout#attr_android:orientation)\nattribute.\n| **Note:** For better performance and tooling support, [build your layout with\n| ConstraintLayout](/training/constraint-layout). Learn more about advantages of using `ConstraintLayout` by reading [Understanding\n| the performance benefits of ConstraintLayout](https://android-developers.googleblog.com/2017/08/understanding-performance-benefits-of.html).\n**Figure 1.** A `LinearLayout` with three horizontally oriented children.\n\nAll children of a `LinearLayout` are stacked one after the other,\nso a vertical list only has one child per row, no matter how wide they are. A\nhorizontal list is only one row high, and it's the height of the tallest child,\nplus padding. A `LinearLayout` respects *margins* between\nchildren, and the *gravity*---right, center, or left\nalignment---of each child.\n\nLayout weight\n-------------\n\n`LinearLayout` also supports assigning a *weight* to\nindividual children with the\n[`android:layout_weight`](/reference/android/widget/LinearLayout.LayoutParams#attr_android:layout_weight)\nattribute. This attribute assigns an \"importance\" value to a view in terms of\nhow much space it occupies on the screen. A larger weight value lets it expand\nto fill the remaining space in the parent view. Child views can specify a weight\nvalue, and any remaining space in the view group is assigned to children\nproportionately, based on their declared weight. The default weight is zero.\n\n### Equal distribution\n\nTo create a linear layout in which each child uses the same amount of space\non the screen, set the\n[`android:layout_height`](/reference/android/view/ViewGroup.LayoutParams#attr_android:layout_height)\nof each view to `\"0dp\"` for a vertical layout, or the\n[`android:layout_width`](/reference/android/view/ViewGroup.LayoutParams#attr_android:layout_width)\nof each view to `\"0dp\"` for a horizontal layout. Then set the\n`android:layout_weight` of each view to `\"1\"`.\n\n### Unequal distribution\n\nYou can also create linear layouts where the child elements use different\namounts of space on the screen. Consider the following examples:\n\n- Suppose you have three text fields: two with a weight value of 1, and a third with the default weight value of 0. The third text field, with the weight value of 0, occupies only the area required by its content. The other two text fields, with the weight value of 1, expand equally to fill the space that remains after the contents of all three fields are measured.\n- If instead you have three text fields where two have a weight value of 1 and the third has a weight of 2, then the space that remains after the contents of all three fields are measured is allocated as follows: half to the field with the weight value of 2, and half divided equally between the fields with the weight value of 1.\n\nThe following figure and code snippet show how layout weights might work in a\n\"send message\" activity. The **To** field, **Subject** line, and\n**Send** button each take up only the height they need. The message area\ntakes up the rest of the activity's height.\n**Figure 2.** Three text fields and a button in a vertically oriented `LinearLayout`. \n\n```xml\n\u003c?xml version=\"1.0\" encoding=\"utf-8\"?\u003e\n\u003cLinearLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"\n android:layout_width=\"match_parent\"\n android:layout_height=\"match_parent\"\n android:paddingLeft=\"16dp\"\n android:paddingRight=\"16dp\"\n android:orientation=\"vertical\" \u003e\n \u003cEditText\n android:layout_width=\"match_parent\"\n android:layout_height=\"wrap_content\"\n android:hint=\"@string/to\" /\u003e\n \u003cEditText\n android:layout_width=\"match_parent\"\n android:layout_height=\"wrap_content\"\n android:hint=\"@string/subject\" /\u003e\n \u003cEditText\n android:layout_width=\"match_parent\"\n android:layout_height=\"0dp\"\n android:layout_weight=\"1\"\n android:gravity=\"top\"\n android:hint=\"@string/message\" /\u003e\n \u003cButton\n android:layout_width=\"100dp\"\n android:layout_height=\"wrap_content\"\n android:layout_gravity=\"end\"\n android:text=\"@string/send\" /\u003e\n\u003c/LinearLayout\u003e\n```\n\nFor details about the attributes available to each child view of a\n`LinearLayout`, see\n[LinearLayout.LayoutParams](/reference/android/widget/LinearLayout.LayoutParams)."]]