Android layout - LinearLayout linear layout

Introduction

Paste the official docs: Linear Layout | Android Developers | Android Developers

Linear means linear, linear, so the layout has the characteristics of a line, either horizontally or vertically.

The layout uses the android:orientation (orientation: positioning) attribute to set whether the sub-controls in the layout are vertical or horizontal. When the border is exceeded, some controls will disappear.

common attributes

Attributes

android:orientation - specifies whether the sub-controls in the layout are vertical or horizontal

value constant value describe
horizontal 0 horizontal layout
vertical 1 vertical layout

android:layout_height - specify the height of the control

android:layout_width - specify the width of the control

Note: The value of this control can also be set as a constant by itself

value

constant value describe
match_parent -1 The control is the same size as the parent control
wrap_content -2 The size based on the content of the control, that is, as big as the content

android:layout_weight - specify the weight of the control *

android:gravity - specifies how the content within the control is positioned

value constant value describe
bottom 50 Heap objects to the bottom of the container without changing the size
center 11

Put the object in the center of the container (horizontally and vertically), without changing the size

center_horizontal 1 center horizontally
center_vertical 10 vertical center
fill 77 fill the container completely
left 3 Push to the left of the container
right 5 Push to the right of the container
top 30 Push to the top of the container
start 800003 push to the beginning of the container
end 800005 pushed to the end of the container

layout weight

In LinearLayout, android:layout_weight can be used to assign weights. This property assigns an " importance " value to a view based on how much space the view should occupy on the screen . With a larger weight value , the view can expand to fill any remaining space in the parent view. Child views can specify a weight value, and any remaining space in the viewgroup is allocated to the child view in proportion to its declared weight value. The default weight is zero.

Even distribution

To make each subview use the same size space, you must set the value of android:layout_weight to 1 for each view.

And if it is a horizontal layout , set the android:layout_width of each view to 0dp ; if it is a vertical layout , set the android:layout_height of each view to 0dp.

Examples are as follows:

horizontal layout

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:orientation="horizontal"
    android:gravity="center"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/purple_200"
        android:text="12345"/>

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/teal_200"
        android:text="12345678910abcdefg"/>

</LinearLayout>

The result is shown in the figure:

vertical layout

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:orientation="vertical"
    android:gravity="center"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="@color/purple_200"
        android:text="12345"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="@color/teal_200"
        android:text="12345"/>

</LinearLayout>

The result is shown in the figure:

Unequal distribution 

 By assigning weights, the larger the number of weights, the more important the view is and the more space it occupies.

If there are three text views, the left and right sides are declared as 1, and the middle is declared as 2, which means that the middle view is more important and takes up more space.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:orientation="horizontal"
    android:gravity="center"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/purple_200"
        android:text="111"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:background="@color/pink"
        android:text="222"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/teal_200"
        android:text="333"/>

</LinearLayout>

The result is shown in the figure:

Examples of Common Properties

 vertical layout

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="1111111"
        android:background="@color/teal_200" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="2222222"
        android:background="@color/purple_200" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="3333333"
        android:background="@color/white" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="4444444"
        android:background="@color/purple_700" />

</LinearLayout>

 horizontal layout

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:orientation="horizontal"
    android:gravity="center"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:text="1111111"
        android:background="@color/teal_200" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:text="2222222"
        android:background="@color/purple_200" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:text="3333333"
        android:background="@color/white" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:text="4444444"
        android:background="@color/purple_700" />

</LinearLayout>

Reference blog: layout_weight usage in Android-0.Android Studio layout_Flower Bear's Blog-CSDN Blog

Guess you like

Origin blog.csdn.net/TDSSS/article/details/126300680