LinearLayout
to grupa widoków, w której wszystkie elementy podrzędne są wyrównane w jednym kierunku, w pionie lub
w poziomie. Kierunek układu możesz określić za pomocą
android:orientation
.
.
.
Rysunek 1.LinearLayout z trzema w poziomie
dzieci z niepełnosprawnością.
Wszystkie elementy podrzędne elementu LinearLayout są układane jeden po drugim.
więc pionowa lista ma tylko 1 element podrzędny na wiersz, niezależnie od szerokości. O
lista pozioma ma tylko 1 wiersz i wysokość najwyższego elementu podrzędnego,
plus dopełnienie. LinearLayout uwzględnia marże pomiędzy
dzieci i grawitację – z prawej, do środka lub lewej,
dla każdego dziecka.
Waga układu
Funkcja LinearLayout obsługuje też przypisywanie wagi do
dla pojedynczych dzieci z
android:layout_weight
. Ten atrybut przypisuje „znaczenie” jako wartości widoku w kontekście
ile miejsca zajmuje na ekranie. Większa wartość wagi umożliwia rozwinięcie
aby wypełnić pozostałe miejsce w widoku nadrzędnym. W widokach podrzędnych można określić wagę
a pozostałe miejsce w grupie widoków jest przypisane do elementów podrzędnych
odpowiednio na podstawie zadeklarowanej wagi. Domyślna waga to 0.
Rozkład równomierny
Aby utworzyć układ liniowy, w którym każdy element podrzędny wykorzystuje taką samą ilość miejsca
ustaw
android:layout_height
każdego widoku do "0dp" w układzie pionowym lub
android:layout_width
każdego widoku do "0dp" w układzie poziomym. Następnie w parametrze
android:layout_weight każdego wyświetlenia do "1".
Nierówny rozkład
Możesz też tworzyć układy liniowe, w których elementy podrzędne różnią się
miejsca na ekranie. Zobacz te przykłady:
Załóżmy, że mamy trzy pola tekstowe: dwa o wartości wagi 1 i
trzeci z domyślną wartością wagi 0. Trzecie pole tekstowe, ze znakiem
wartość wagi 0 zajmuje tylko obszar wymagany przez jej treść. Druga
po dwóch polach tekstowych o wartości wagi 1 rozciągną się równomiernie, aby wypełnić
przestrzeń pozostającą po pomiarze zawartości wszystkich 3 pól.
Jeśli zamiast tego masz 3 pola tekstowe, z których dwa mają wartość wagi 1
a trzecia – 2 wagę, i spacja pozostająca po
wartości ze wszystkich trzech pól są mierzone w następujący sposób: połowa do
pole o wartości wagi 2, które jest podzielone po równo między
pól o wartości wagi 1.
Poniższy rysunek i fragment kodu pokazują, jak mogą działać wagi układu w
„wyślij wiadomość” działania. w polu Do, w wierszu Temat i
Każdy przycisk Wyślij zajmuje tylko odpowiednią wysokość. Obszar wiadomości
zajmuje pozostałą część wysokości aktywności.
Rysunek 2. Trzy pola tekstowe i przycisk w orientacji pionowej
LinearLayout
Szczegółowe informacje o atrybutach dostępnych dla poszczególnych widoków podrzędnych
LinearLayout, zobacz
LinearLayout.LayoutParams
Treść strony i umieszczone na niej fragmenty kodu podlegają licencjom opisanym w Licencji na treści. Java i OpenJDK są znakami towarowymi lub zastrzeżonymi znakami towarowymi należącymi do firmy Oracle lub jej podmiotów stowarzyszonych.
Ostatnia aktualizacja: 2025-07-27 UTC.
[[["Łatwo zrozumieć","easyToUnderstand","thumb-up"],["Rozwiązało to mój problem","solvedMyProblem","thumb-up"],["Inne","otherUp","thumb-up"]],[["Brak potrzebnych mi informacji","missingTheInformationINeed","thumb-down"],["Zbyt skomplikowane / zbyt wiele czynności do wykonania","tooComplicatedTooManySteps","thumb-down"],["Nieaktualne treści","outOfDate","thumb-down"],["Problem z tłumaczeniem","translationIssue","thumb-down"],["Problem z przykładami/kodem","samplesCodeIssue","thumb-down"],["Inne","otherDown","thumb-down"]],["Ostatnia aktualizacja: 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)."]]