Summary of Common Attributes of LinearLayout Layout in Android

  • 2021-07-01 08:10:47
  • OfStack

Basic attribute requirements


<LinearLayout
  android:layout_width="match_parent"
  android:layout_height="match_parent"

  android:orientation="vertical">
 </LinearLayout>

android:orientation Decide whether to arrange horizontally or vertically vertical Vertical Arrangement horizontal horizontal arrangement

Vertically aligned Button


<LinearLayout
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical" >

  <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button 1" />
  <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button 2" />

</LinearLayout>

Horizontal arrangement Button


<LinearLayout
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical" >

  <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button 1" />
  <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button 2" />

</LinearLayout>

Gravity center setting


<LinearLayout
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"

  android:gravity="left">
</LinearLayout>

android:gravity Set the placement direction of the contents of the frame center is centered horizontally and vertically center_vertical Vertical Center center_horizontal Horizontal Center top topping left left bottom Bottom right set to the right

Horizontal and vertical centering


<LinearLayout
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  android:gravity="center_vertical">

  <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button 1" />

</LinearLayout>

<LinearLayout
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  android:gravity="center_horizontal">

  <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button 1" />

</LinearLayout>

<LinearLayout
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  android:gravity="center">

  <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button 1" />

</LinearLayout>

Combination of barycenter through OR operator


<LinearLayout
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  android:gravity="top|right">

  <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button 1" />

</LinearLayout>

<LinearLayout
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  android:gravity="bottom|left">

  <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button 1" />

</LinearLayout>

<LinearLayout
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  android:gravity="center_vertical|center_horizontal">

  <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button 1" />

</LinearLayout>

Proportional distribution


<LinearLayout
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="horizontal">

  <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button 1"

    android:layout_weight="1"/>

</LinearLayout>

android:layout_weight Specific gravity of sub-components or sub-frames. This property can only be set if the child component or child frame under LinearLayout.

Equal proportion distribution


<LinearLayout
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="horizontal">

  <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button 1"
    android:layout_weight="1"/>

  <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button 2"
    android:layout_weight="1"/>

  <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button 3"
    android:layout_weight="1"/>

</LinearLayout>

The specific gravity is all 1, so the size is the same.


Unequal proportional distribution


<LinearLayout
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="horizontal">

  <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button 1"
    android:layout_weight=".10"/>

  <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button 2"
    android:layout_weight=".20"/>
  <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button 3"
    android:layout_weight=".70"/>

</LinearLayout>

. 10 stands for 0.10
. 20 stands for 0.20
. 70 stands for 0.70
It adds up to exactly 1, making 100% distribution.


Related articles: