LinearLayout
היא קבוצת תצוגה שמיישרת את כל הילדים בכיוון אחד, באופן אנכי או
לרוחב. אפשר לציין את כיוון הפריסה באמצעות
android:orientation
.
איור 1.LinearLayout עם שלוש לרוחב
לילדים ממוקדי-עין.
כל הצאצאים של LinearLayout מוערם אחד אחרי השני,
כך שברשימה אנכית יש רק צאצא אחד בכל שורה, ללא קשר לרוחב שלהם. א'
הרשימה האופקית היא בגובה של שורה אחת בלבד, וזה הגובה של הילד או הילדה הכי גבוהים,
עם מרווח פנימי. LinearLayout מכבד שוליים בין
ילדים, וכוח הכבידה - ימין, מרכז או שמאל
להתאמה אישית של כל ילד וילד.
עובי הפריסה
LinearLayout תומך גם בהקצאת משקל
לילדים בודדים
android:layout_weight
. למאפיין הזה מוקצה 'חשיבות' ערך לצפייה במונחים של
כמה מקום הוא תופס במסך. ערך משקל גדול יותר מאפשר להרחיב
כדי למלא את השטח הנותר בתצוגת ההורה. בתצוגות צאצא ניתן לציין משקל
וכל השטח הנותר בקבוצה של התצוגה המפורטת מוקצה לילדים
באופן יחסי, על סמך המשקל המוצהר שלהם. משקל ברירת המחדל הוא אפס.
התפלגות שווה
כדי ליצור פריסה לינארית שבה כל ילד וילדה ישתמשו באותו שטח
במסך, מגדירים
android:layout_height
של כל תצוגה ל-"0dp" בפריסה אנכית, או
android:layout_width
של כל תצוגה ל-"0dp" לפריסה אופקית. לאחר מכן מגדירים את
android:layout_weight מכל צפייה אל "1".
התפלגות לא שוויונית
תוכלו גם ליצור פריסות ליניאריות שבהן רכיבי הצאצא משתמשים
כמויות של שטח במסך. מומלץ להביא בחשבון את הדוגמאות הבאות:
נניח שיש לכם שלושה שדות טקסט: שניים עם ערך משקל של 1, ושדה
שלישי, שערך ברירת המחדל שלו הוא 0. בשדה הטקסט השלישי, עם המאפיין
ערך של 0, כולל רק את האזור שנדרש על ידי התוכן שלו. השני
שני שדות טקסט, שבהם ערך המשקל של 1, מתרחבים באופן שווה כדי למלא את
של השטח שנשאר אחרי מדידת התוכן של כל שלושת השדות.
אם במקום זאת יש לכם שלושה שדות טקסט שבהם לשניים יש ערך משקל של 1
ובשלישי יש משקל 2, אז הרווח שנשאר אחרי
התוכן של כל שלושת השדות נמדד באופן הבא: חצי ל-
בשדה שערך המשקל שלו הוא 2, וחצי חלקי באופן שווה
שדות עם ערך המשקל 1.
האיור וקטע הקוד הבאים מראים כיצד משקלי הפריסה עשויים לפעול
"send message" פעילות. השדה אל, שורת נושא ו
כל לחצן שלח תופס רק לגובה הרצוי. אזור ההודעה
תופסת את שאר הגובה של הפעילות.
איור 2. שלושה שדות טקסט ולחצן בכיוון אנכי
LinearLayout
דוגמאות התוכן והקוד שבדף הזה כפופות לרישיונות המפורטים בקטע רישיון לתוכן. Java ו-OpenJDK הם סימנים מסחריים או סימנים מסחריים רשומים של חברת Oracle ו/או של השותפים העצמאיים שלה.
עדכון אחרון: 2025-07-27 (שעון UTC).
[[["התוכן קל להבנה","easyToUnderstand","thumb-up"],["התוכן עזר לי לפתור בעיה","solvedMyProblem","thumb-up"],["סיבה אחרת","otherUp","thumb-up"]],[["חסרים לי מידע או פרטים","missingTheInformationINeed","thumb-down"],["התוכן מורכב מדי או עם יותר מדי שלבים","tooComplicatedTooManySteps","thumb-down"],["התוכן לא עדכני","outOfDate","thumb-down"],["בעיה בתרגום","translationIssue","thumb-down"],["בעיה בדוגמאות/בקוד","samplesCodeIssue","thumb-down"],["סיבה אחרת","otherDown","thumb-down"]],["עדכון אחרון: 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)."]]