Android constraint layout nesting is so easy

We want to complete such a UI

 

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:context=".MainActivity">


    <LinearLayout
        android:id="@+id/target"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_margin="20dp"
        android:background="@android:color/holo_green_dark"
        android:orientation="horizontal"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <LinearLayout
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:background="@android:color/holo_green_light"
        android:orientation="horizontal"
        app:layout_constraintEnd_toEndOf="@id/target"
        app:layout_constraintStart_toStartOf="@id/target"
        app:layout_constraintTop_toBottomOf="@id/target"
        app:layout_constraintBottom_toBottomOf="@id/target"

        />

</androidx.constraintlayout.widget.ConstraintLayout>

 Effect

 

The following two sentences are difficult to understand

        app:layout_constraintTop_toBottomOf="@id/target"
        app:layout_constraintBottom_toBottomOf="@id/target"

Then we delete one, don't you understand?

 deleting a

 So top to bottom and bottom to top. They are bound. Naturally run to the middle. very stylish

Guess you like

Origin blog.csdn.net/mp624183768/article/details/124441475