[Android] The most complete analysis of linear layout (LinearLayout)
1. Overview of LinearLayout
线性布局(LinearLayout)
Mainly arrange the controls in the interface horizontally or vertically. and arrange the controls into a straight line. In a linear layout, if arranged horizontally, only one control can be placed vertically, and if arranged vertically, only one control can be placed horizontally.
To use linear layout, you need to change the layout node to LinearLayout
, the basic format is as follows:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
....
</LinearLayout>
2. Common properties of LinearLayout
2.1 orientation attribute
In linear layout, controls are arranged in two directions, horizontal and vertical, and the direction of control arrangement is android:orientation
controlled by an attribute, which needs to be added to LinearLayout
the attribute of the mark.
As can be seen from the figure above, if orientation
the property value is set to horizontal
, the controls will 从左往右
be arranged in the horizontal direction, and if orientation
the property value is set to , vertical
the controls will be arranged in the vertical direction 从上往下
.
2.2 Gravity property
The controls in the linear layout are 从左往右
arranged or 从上往下
aligned by default. If you want the controls in the linear layout to be aligned to the right edge or bottom, you can use gravity
properties to control them.
However, this attribute value is not only LinearLayout
available in , other layouts can also take effect with this attribute.
2.3 layout_weight attribute
LinearLayout
Another commonly used attribute in is layout_weight
that this attribute needs to be added to LinearLayout
the child control. Its role is to allocate the remaining space in the linear layout to the control.
As shown in the figure below, when no layout_weight
attributes are added to the control, the area where the control does not occupy the linear layout will be vacated.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="400dp"
android:layout_height="300dp"
android:background="@color/teal_200"
android:orientation="horizontal"
tools:context=".MainActivity">
<Button
android:id="@+id/button"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
After adding the control button2 android:layout_weight="1"
, it will fill up the remaining space of the linear layout.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="400dp"
android:layout_height="300dp"
android:background="@color/teal_200"
android:orientation="horizontal"
tools:context=".MainActivity">
<Button
android:id="@+id/button"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="@+id/button2"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
If button1
both are given and button2
added android:layout_weight="1"
, the two controls will evenly distribute the remaining space.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="400dp"
android:layout_height="300dp"
android:background="@color/teal_200"
android:orientation="horizontal"
tools:context=".MainActivity">
<Button
android:id="@+id/button1"
android:layout_width="100dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="@+id/button2"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
If you button1
add android:layout_weight="3"
, button2
add android:layout_weight="1"
, since the remaining space is 200, button1
and button2
will allocate the remaining space in a ratio of 3:1.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="400dp"
android:layout_height="300dp"
android:background="@color/teal_200"
android:orientation="horizontal"
tools:context=".MainActivity">
<Button
android:id="@+id/button1"
android:layout_width="100dp"
android:layout_weight="3"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="@+id/button2"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>