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 تقسیم می شود.
شکل زیر و قطعه کد نشان میدهد که وزن طرحبندی چگونه ممکن است در فعالیت «ارسال پیام» کار کند. فیلد To ، خط موضوع و دکمه Send هر کدام فقط ارتفاع مورد نیاز خود را می گیرند. ناحیه پیام بقیه ارتفاع فعالیت را اشغال می کند.
شکل 2. سه فیلد متنی و یک دکمه در یک LinearLayout عمودی گرا.
برای جزئیات در مورد ویژگی های موجود برای هر نمای فرزند LinearLayout ، به LinearLayout.LayoutParams مراجعه کنید.
محتوا و نمونه کدها در این صفحه مشمول پروانههای توصیفشده در پروانه محتوا هستند. جاوا و OpenJDK علامتهای تجاری یا علامتهای تجاری ثبتشده Oracle و/یا وابستههای آن هستند.
تاریخ آخرین بهروزرسانی 2025-07-29 بهوقت ساعت هماهنگ جهانی.
[[["درک آسان","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-29 بهوقت ساعت هماهنگ جهانی."],[],[],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)."]]