LinearLayout
adalah kelompok tampilan yang menyejajarkan semua turunan dalam satu arah, secara vertikal atau
secara horizontal. Anda dapat menetapkan arah tata letak dengan
android:orientation
.
Gambar 1.LinearLayout dengan tiga secara horizontal
yang berorientasi pada anak.
Semua turunan LinearLayout ditumpuk satu per satu,
jadi daftar vertikal hanya memiliki satu turunan per baris, tidak peduli seberapa lebarnya. J
daftar horizontal tingginya hanya satu baris, dan itu adalah
tinggi dari turunan tertinggi,
plus padding. LinearLayout mengikuti margin antara
anak-anak, dan gravitasi—kanan, tengah, atau kiri
keselarasan—setiap turunan.
Tebal tata letak
LinearLayout juga mendukung penetapan bobot ke
setiap anak dengan
android:layout_weight
. Atribut ini menetapkan nilai "penting" nilai pada tampilan dalam hal
berapa banyak ruang yang
ditempatkan di layar. Nilai bobot yang lebih besar memungkinkannya meluas
untuk mengisi ruang yang tersisa di tampilan induk. Tampilan turunan dapat menentukan bobot
nilai, dan ruang yang tersisa dalam kelompok tampilan akan ditetapkan ke turunan
secara proporsional, berdasarkan bobot yang dinyatakan. Bobot default adalah nol.
Distribusi setara
Untuk membuat tata letak linear yang setiap turunannya menggunakan jumlah ruang yang sama
di layar, atur
android:layout_height
setiap tampilan menjadi "0dp" untuk tata letak vertikal, atau
android:layout_width
setiap tampilan menjadi "0dp" untuk tata letak horizontal. Kemudian, tetapkan
android:layout_weight dari setiap tampilan ke "1".
Distribusi tidak setara
Anda juga dapat membuat tata letak linear di mana elemen turunan menggunakan
ukuran ruang di layar. Perhatikan contoh berikut:
Misalkan Anda memiliki tiga bidang teks: dua dengan nilai bobot 1, dan
ketiga dengan nilai bobot {i>default<i} 0. Isian teks ketiga, dengan
nilai bobot 0, hanya menempati area yang dibutuhkan oleh kontennya. Lainnya
dua bidang teks, dengan nilai bobot 1, luaskan secara merata untuk mengisi
ruang yang tersisa setelah isi ketiga isian diukur.
Jika, Anda memiliki tiga kolom teks yang mana dua memiliki nilai bobot 1
dan yang ketiga memiliki bobot 2, lalu ruang yang tersisa setelah
isi dari ketiga isian diukur dialokasikan sebagai berikut: setengah sampai
isian dengan nilai bobot 2, dan separuhnya dibagi sama antara
isian dengan nilai
bobot 1.
Gambar dan cuplikan kode berikut menunjukkan cara kerja bobot tata letak dalam
"kirim pesan" aktivitas Anda. Kolom Kepada, baris Subjek, dan
tombol Kirim masing-masing hanya mengambil ketinggian yang diperlukan. Area pesan
akan menempati sisa tinggi aktivitas.
Gambar 2. Tiga kolom teks dan satu tombol dalam orientasi vertikal
LinearLayout.
Untuk detail tentang atribut yang tersedia bagi setiap tampilan turunan
LinearLayout, lihat
LinearLayout.LayoutParams.
Konten dan contoh kode di halaman ini tunduk kepada lisensi yang dijelaskan dalam Lisensi Konten. Java dan OpenJDK adalah merek dagang atau merek dagang terdaftar dari Oracle dan/atau afiliasinya.
Terakhir diperbarui pada 2025-07-27 UTC.
[[["Mudah dipahami","easyToUnderstand","thumb-up"],["Memecahkan masalah saya","solvedMyProblem","thumb-up"],["Lainnya","otherUp","thumb-up"]],[["Informasi yang saya butuhkan tidak ada","missingTheInformationINeed","thumb-down"],["Terlalu rumit/langkahnya terlalu banyak","tooComplicatedTooManySteps","thumb-down"],["Sudah usang","outOfDate","thumb-down"],["Masalah terjemahan","translationIssue","thumb-down"],["Masalah kode / contoh","samplesCodeIssue","thumb-down"],["Lainnya","otherDown","thumb-down"]],["Terakhir diperbarui pada 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)."]]