LinearLayout
عبارة عن مجموعة عرض تربط جميع الأطفال في اتجاه واحد، عموديًا أو
أفقيًا. يمكنك تحديد اتجاه التخطيط باستخدام
android:orientation
.
الشكل 1.LinearLayout مع ثلاثة أفقيًا
توجيه الأطفال.
يتم تكديس جميع عناصر LinearLayout واحدًا تلو الآخر،
لذلك فإن القائمة الرأسية يكون لها عنصر ثانوي واحد فقط في كل صف، بغض النظر عن مدى عرضها. حاسمة
القائمة الأفقية بارتفاع صف واحد فقط، وهو طول أطول طفل،
بالإضافة إلى المساحة المتروكة. تأخذ LinearLayout في الاعتبار الهوامش بين
الأطفال والجاذبية: يمين أو مركز أو يسار
المحاذاة - لكل طفل.
عرض التصميم
تتيح LinearLayout أيضًا تعيين الوزن إلى
أفراد من الأطفال لديهم
android:layout_weight
. تحدد هذه السمة "أهمية" القيمة إلى المشاهدة بالنسبة إلى
مقدار المساحة التي يشغلها على الشاشة. تسمح قيمة الوزن الأكبر بالتوسيع
لملء المساحة المتبقية في طريقة العرض الأصلية. يمكن أن تحدد طرق العرض الفرعية الوزن
وأي مساحة متبقية في مجموعة العرض مخصصة للأطفال
بشكل يتناسب، بناءً على وزنها المعلن. ويكون الوزن التلقائي صفرًا.
توزيع متساوٍ
لإنشاء تخطيط خطي يستخدم فيه كل طفل نفس القدر من المساحة
على الشاشة، اضبط
android:layout_height
من كل طريقة عرض حتى "0dp" للتخطيط العمودي، أو
android:layout_width
من كل طريقة عرض حتى "0dp" للتنسيق الأفقي. ثم اضبط
android:layout_weight من كل مشاهدة حتى "1".
توزيع غير متساوٍ
يمكنك أيضًا إنشاء تخطيطات خطية حيث تستخدم العناصر الفرعية بشكل مختلف
مقدار المساحة على الشاشة. فكِّر في الأمثلة التالية:
افترض أن لديك ثلاثة حقول نصية: اثنان بقيمة ترجيح 1، وa
الثالثة بقيمة الوزن الافتراضية وهي 0. يمكن للحقل النصي الثالث،
قيمة الوزن 0، فلا تشغل سوى المساحة المطلوبة بواسطة محتواها. الآخر
حقلين نصيين، بقيمة الترجيح 1، وتوسيعهما بالتساوي لملء
المساحة المتبقية بعد قياس محتويات الحقول الثلاثة جميعها.
إذا كان لديك بدلاً من ذلك ثلاثة حقول نصية، حيث يكون لاثنان منها قيمة ترجيح 1
ووزن الثالث 2، فإن المسافة المتبقية بعد
يتم تخصيص محتوى جميع الحقول الثلاثة على النحو التالي: نصف إلى
الحقل بقيمة الوزن 2، ونصفها مقسّمًا بالتساوي بين
الحقول التي لها قيمة الوزن 1.
يوضح الشكل ومقتطف الرمز التاليان كيف يمكن عمل ترجيحات التصميم في
"إرسال رسالة" الأخرى. الحقل إلى وسطر الموضوع
يشغل كلّ من زر إرسال الارتفاع المطلوب فقط. منطقة الرسالة
ويشغل بقية ارتفاع النشاط.
الشكل 2. ثلاثة حقول نصية وزر في اتجاه رأسي
LinearLayout
للحصول على تفاصيل حول السمات المتاحة لكل طريقة عرض فرعية
LinearLayout، عرض
LinearLayout.LayoutParams
يخضع كل من المحتوى وعيّنات التعليمات البرمجية في هذه الصفحة للتراخيص الموضحّة في ترخيص استخدام المحتوى. إنّ Java وOpenJDK هما علامتان تجاريتان مسجَّلتان لشركة Oracle و/أو الشركات التابعة لها.
تاريخ التعديل الأخير: 2025-07-27 (حسب التوقيت العالمي المتفَّق عليه)
[[["يسهُل فهم المحتوى.","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 (حسب التوقيت العالمي المتفَّق عليه)"],[],[],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)."]]