LinearLayout
est un groupe de vues qui aligne tous les enfants dans une seule direction, verticalement ou
horizontalement. Vous pouvez spécifier la direction de la mise en page à l'aide de la commande
android:orientation
.
<ph type="x-smartling-placeholder">
<ph type="x-smartling-placeholder"></ph>
<ph type="x-smartling-placeholder"></ph>
Figure 1 : Un élément LinearLayout avec trois horizontalement
pour les enfants plus ambitieux.
Tous les enfants d'une LinearLayout sont empilés les uns après les autres.
Une liste verticale ne contient donc qu'un seul enfant par ligne, quelle que soit leur largeur. A
liste horizontale d'une seule ligne, et qui correspond à la hauteur de l'enfant le plus grand,
ainsi qu'une marge intérieure. Un LinearLayout respecte les marges entre
les enfants et la gravité (droit, centre ou gauche)
de chaque enfant.
Épaisseur de la mise en page
LinearLayout permet également d'attribuer une pondération aux
pour chaque enfant ayant
android:layout_weight
. Cet attribut attribue une "importance" à une vue en termes de
l'espace qu'elle occupe sur l'écran. Plus la valeur de pondération est élevée,
pour remplir l'espace restant dans la vue parent. Les vues enfants peuvent spécifier une pondération
et tout espace restant dans le groupe de vues est attribué aux enfants
proportionnellement, en fonction de leur pondération déclarée. La pondération par défaut est zéro.
Distribution égale
Pour créer une mise en page linéaire dans laquelle chaque enfant utilise la même quantité d'espace
à l'écran, définissez le paramètre
android:layout_height
de chaque vue sur "0dp" pour une mise en page verticale, ou la
android:layout_width
de chaque vue sur "0dp" pour une mise en page horizontale. Définissez ensuite
android:layout_weight de chaque vue à "1".
Distribution inégale
Vous pouvez également créer des mises en page linéaires dans lesquelles les éléments enfants utilisent différentes
la quantité d'espace à l'écran. Exemples :
Supposons que vous ayez trois champs de texte: deux avec une pondération de 1, et un champ
avec la valeur de pondération
par défaut de 0. Le troisième champ de texte,
de 0, n'occupe que la surface requise par son contenu. L'autre
deux champs de texte, d'une valeur de pondération de 1, se développer de manière égale pour remplir
l'espace restant après la mesure
du contenu des trois champs.
Si, au contraire, vous avez trois champs de texte dont deux ont une valeur de pondération de 1
et le troisième a une pondération de 2, l'espace restant après
le contenu des trois champs est mesuré comme suit :
champ dont la valeur de pondération est 2, et la moitié divisée à parts égales entre
avec une valeur de pondération de 1.
La figure et l'extrait de code suivants montrent comment les pondérations de mise en page peuvent fonctionner dans un
"envoyer un message" activité. Le champ À, la ligne Objet et
Send (Envoyer) n'occupent que la hauteur dont ils ont besoin. Zone du message
occupe le reste de la taille de l'activité.
Figure 2 : Trois champs de texte et un bouton dans une orientation verticale
LinearLayout
Pour en savoir plus sur les attributs disponibles pour chaque vue enfant d'une
LinearLayout, consultez
LinearLayout.LayoutParams
Le contenu et les exemples de code de cette page sont soumis aux licences décrites dans la Licence de contenu. Java et OpenJDK sont des marques ou des marques déposées d'Oracle et/ou de ses sociétés affiliées.
Dernière mise à jour le 2025/07/27 (UTC).
[[["Facile à comprendre","easyToUnderstand","thumb-up"],["J'ai pu résoudre mon problème","solvedMyProblem","thumb-up"],["Autre","otherUp","thumb-up"]],[["Il n'y a pas l'information dont j'ai besoin","missingTheInformationINeed","thumb-down"],["Trop compliqué/Trop d'étapes","tooComplicatedTooManySteps","thumb-down"],["Obsolète","outOfDate","thumb-down"],["Problème de traduction","translationIssue","thumb-down"],["Mauvais exemple/Erreur de code","samplesCodeIssue","thumb-down"],["Autre","otherDown","thumb-down"]],["Dernière mise à jour le 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)."]]