Basic use of ConstraintLayout layout

Android Basic Layout Constraint Layout

Google I/O 2016 released Constraint Layout, which is said to be very powerful. Let's explore it.

gradle configuration

      compile 'com.android.support.constraint:constraint-layout:1.0.0-beta2'  

Reading Prerequisites: Familiarity with the Four Basic Layouts

1. Position control 8 boundary control attributes
Note: The leftmost side represents the leftmost movable side and the left side represents the left boundary of the View.
    app:layout_constraintLeft_toLeftOf 
    app:layout_constraintLeft_toRightOf The position on my left is similar to the one on the other's right.
    app:layout_constraintRight_toRightOf 
    app:layout_constraintRight_toLeftOf
    app:layout_constraintTop_toTopOf 
    app:layout_constraintTop_toBottomOf
    app:layout_constraintBottom_toBottomOf
    app:layout_constraintBottom_toTopOf

It doesn't matter if you don't understand. Look at the examples.

* For example:

    <!--As shown in the figure, there is one on the left. A,The one on the right C,If I want to build a new one B stay A C Between, as follows-->
     <Button
        app:layout_constraintLeft_toRightOf="@+id/bt_a"
        app:layout_constraintRight_toLeftOf="@+id/bt_c"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="B"/>
    <!--Literally: 1.My leftmost position, in button A To the right of the-->
    <!--Literally: 1.My rightmost position, in button C On the left-->

As shown in the figure above, the position of the leftmost and rightmost has been determined. B appears in the middle of A and C, but what if I don't want to be in the middle (for example, I want to be on the right).

Two migration attributes are introduced here.
    layout_constraintHorizontal_bias (horizontal migration) (range 0-1)
    layout_constraintVertical_bias (vertical migration) (range 0-1)  

How to understand? I just try not to talk. Look at the picture.
As shown in Figure B, the horizontal offset of B is 0.

    app:layout_constraintHorizontal_bias="0"

As shown in Figure B, the horizontal offset of B is 0.5.

    app:layout_constraintHorizontal_bias="0.5"

As shown in Figure B, the horizontal offset of B is 0.7.

    app:layout_constraintHorizontal_bias="0.7"

As shown in Figure B, the horizontal offset of B is 1.

    app:layout_constraintHorizontal_bias="1"    

Summary: Do you understand? If you don't understand, please continue to see the picture.
1. Fixed the leftmost, rightmost, top and bottom position of View by eight boundary constraint attributes.
2. By setting the offset property, you can control the View moving in the boundary area. The left-most side is 0, the right-most side is 1, and the middle is 0.5.
3. When the boundary constraint property is set, View automatically appears in the middle, that is to say, the default offset property is 0.5.

2. Size control first introduces two layout size control attributes
    layout_constraintHorizontal_weight //Proportion in horizontal direction, similar to linear layout
    layout_constraintVertical_weight  //Vertical gravity, similar to linear layout

Next, I'll use Constraint Layout to mimic an example of a horizontal linear layout.

Complete layout file:

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout 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:ignore="MissingConstraints">
        <!--A Boundary control attributes are left and right-->
        <Button
            android:id="@+id/bt_a"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="A"
            app:layout_constraintHorizontal_weight="1"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toLeftOf="@id/bt_b"/>
        <!--B Boundary control attributes also have left and right attributes-->
        <Button
            android:id="@+id/bt_b"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="B"
            app:layout_constraintHorizontal_weight="1"
            app:layout_constraintLeft_toRightOf="@id/bt_a"
            app:layout_constraintRight_toLeftOf="@id/bt_c"/>
        <!--C The boundary control property is only right-->
        <Button
            android:id="@+id/bt_c"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="C"
            app:layout_constraintHorizontal_weight="1"
            app:layout_constraintRight_toRightOf="parent"/>
    </android.support.constraint.ConstraintLayout>

The effect is as follows (C is obviously smaller in the rendering, indicating that the C ratio setting has no effect)

Conclusion:
1. To achieve horizontal linear layout, all View s must have left and right boundary control attributes and control each other.
2. To realize the control of proportion and size, layout_width="0dp" must be set.

Figure layout (which basically means that you have mastered the control of specific gravity)

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout 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:ignore="MissingConstraints">
        <TextView
            android:background="#0f0"
            android:id="@+id/bt_a"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:text="A"
            app:layout_constraintBottom_toTopOf="@id/bt_b"
            app:layout_constraintHorizontal_weight="1"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toLeftOf="@id/bt_b"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_weight="1"/>
        <TextView
            android:background="#0f0"
            android:id="@+id/bt_b"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:text="B"
            app:layout_constraintBottom_toTopOf="@id/bt_c"
            app:layout_constraintHorizontal_weight="1"
            app:layout_constraintLeft_toRightOf="@id/bt_a"
            app:layout_constraintRight_toLeftOf="@id/bt_c"
            app:layout_constraintTop_toBottomOf="@id/bt_a"
            app:layout_constraintVertical_weight="1"/>
        <TextView
            android:background="#0f0"
            android:id="@+id/bt_c"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:text="C"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintHorizontal_weight="1"
            app:layout_constraintLeft_toRightOf="@id/bt_b"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@id/bt_b"
            app:layout_constraintVertical_weight="1"/>
    </android.support.constraint.ConstraintLayout>

The results are as follows:

3. Supplementary Position Control

Absolute coordinates: The upper left corner of the parent layout defaults to (0, 0), which is the coordinate relative to the upper left corner of the parent layout.

    layout_editor_absoluteX absolute coordinate x
    layout_editor_absoluteY absolute coordinate y

When the left boundary control property is set, the x absolute coordinates fail. Use the layout_marginLeft instead.
When the upper boundary control property is set, y absolute coordinates fail. Use layout_marginTop instead.

So, absolute coordinates... Not well adapted, not well controlled. Negative comment.

A separate sheet is attached.
Android Studio Super Intelligent Control Settings Diagram, as follows

This is the full text of Constraint Layout of Android Basic Layout. I hope it will be helpful for you to learn Android application development.

Keywords: Android xml encoding Google

Added by capetonian on Thu, 04 Jul 2019 23:14:08 +0300