LinearLayout
è un gruppo di viste che allinea tutti gli elementi secondari in un'unica direzione, verticalmente
in orizzontale. Puoi specificare la direzione del layout con
android:orientation
.
di Gemini Advanced.
Figura 1. Un LinearLayout con tre orizzontalmente
orientati ai bambini.
Tutti gli elementi secondari di LinearLayout vengono sovrapposti uno dopo l'altro,
pertanto un elenco verticale ha un solo elemento secondario per riga, indipendentemente dalla loro larghezza. R
dell'elenco orizzontale
è alto una sola riga ed è l'altezza del profilo secondario più alto,
più spaziatura interna. Un LinearLayout rispetta i margini tra
bambini e la gravità (destra, centro o sinistra)
di ogni elemento figlio.
Spessore layout
LinearLayout supporta anche l'assegnazione di un peso a
singoli bambini con
android:layout_weight
. Questo attributo assegna un'"importanza" a una vista in termini di
da quanto spazio occupa sullo schermo. Un valore di ponderazione più grande consente di espandere
per riempire lo spazio rimanente nella vista genitore. Le viste secondarie possono specificare una ponderazione
e l'eventuale spazio rimanente nel gruppo di visualizzazioni viene assegnato agli elementi secondari
in modo proporzionale, in base al peso dichiarato. Il peso predefinito è zero.
Distribuzione uguale
Per creare un layout lineare in cui ogni asset secondario utilizzi la stessa quantità di spazio
sullo schermo, imposta
android:layout_height
di ogni vista a "0dp" per un layout verticale, oppure la
android:layout_width
di ogni vista a "0dp" per un layout orizzontale. Quindi imposta
android:layout_weight di ogni visualizzazione per "1".
Distribuzione disuguale
Puoi anche creare layout lineari in cui gli elementi secondari utilizzano diversi
quantità di spazio libero sullo schermo. Considera i seguenti esempi:
Supponi di avere tre campi di testo: due con un valore di ponderazione pari a 1 e un
con il valore predefinito di 0. Il terzo campo di testo, con
un valore di ponderazione pari a 0, occupa solo l'area richiesta dai suoi contenuti. L'altro
due campi di testo, con valore di ponderazione pari a 1, si espandono equamente per riempire
spazio rimanente dopo la misurazione dei contenuti di tutti e tre i campi.
Se invece hai tre campi di testo di cui due hanno un valore di ponderazione pari a 1
mentre il terzo ha un peso pari a 2, lo spazio rimanente dopo
contenuto di tutti e tre i campi viene allocato come segue:
il campo con valore di ponderazione pari a 2 e metà diviso equamente tra
con un valore di ponderazione pari a 1.
La figura e lo snippet di codice riportati di seguito mostrano come potrebbero funzionare le ponderazioni del layout in un
"invia messaggio" attività. Il campo A, la riga Oggetto e
Ogni pulsante Invia occupa solo l'altezza necessaria. L'area del messaggio
occupa il resto dell'altezza dell'attività.
.
Figura 2. Tre campi di testo e un pulsante in posizione verticale
LinearLayout.
Per maggiori dettagli sugli attributi disponibili per ogni vista secondaria di un
LinearLayout, vedi
LinearLayout.LayoutParams.
I campioni di contenuti e codice in questa pagina sono soggetti alle licenze descritte nella Licenza per i contenuti. Java e OpenJDK sono marchi o marchi registrati di Oracle e/o delle sue società consociate.
Ultimo aggiornamento 2025-07-27 UTC.
[[["Facile da capire","easyToUnderstand","thumb-up"],["Il problema è stato risolto","solvedMyProblem","thumb-up"],["Altra","otherUp","thumb-up"]],[["Mancano le informazioni di cui ho bisogno","missingTheInformationINeed","thumb-down"],["Troppo complicato/troppi passaggi","tooComplicatedTooManySteps","thumb-down"],["Obsoleti","outOfDate","thumb-down"],["Problema di traduzione","translationIssue","thumb-down"],["Problema relativo a esempi/codice","samplesCodeIssue","thumb-down"],["Altra","otherDown","thumb-down"]],["Ultimo aggiornamento 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)."]]