Android ConstraintLayout Constraint layout Flow flow layout, table layout

Table layout in the four major layouts of Android. Only used during interviews.

So how does Constraint Layout replace him?

<?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">


    <androidx.constraintlayout.helper.widget.Flow
        android:id="@+id/flow1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:constraint_referenced_ids="tv1,tv2,tv3,tv4,tv5,tv6"
        app:flow_horizontalGap="5dp"
        app:flow_maxElementsWrap="4"
        app:flow_verticalGap="5dp"
        app:flow_verticalStyle="packed"
        app:flow_wrapMode="aligned"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0"

        />

    <TextView
        android:id="@+id/tv1"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:background="@android:color/holo_red_dark"
        android:gravity="center"
        android:text="1"
        android:textColor="#fff"
        android:textSize="24sp" />

    <TextView
        android:id="@+id/tv2"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:background="@android:color/holo_red_dark"
        android:gravity="center"
        android:text="2"
        android:textColor="#fff"
        android:textSize="24sp" />

    <TextView
        android:id="@+id/tv3"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:background="@android:color/holo_red_dark"
        android:gravity="center"
        android:text="3"
        android:textColor="#fff"
        android:textSize="24sp" />

    <TextView
        android:id="@+id/tv4"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:background="@android:color/holo_red_dark"
        android:gravity="center"
        android:text="4"
        android:textColor="#fff"
        android:textSize="24sp" />

    <TextView
        android:id="@+id/tv5"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:background="@android:color/holo_red_dark"
        android:gravity="center"
        android:text="5"
        android:textColor="#fff"
        android:textSize="24sp" />

    <TextView
        android:id="@+id/tv6"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:background="@android:color/holo_red_dark"
        android:gravity="center"
        android:text="6"
        android:textColor="#fff"
        android:textSize="24sp" />

</androidx.constraintlayout.widget.ConstraintLayout>

The core is that other layouts don't need to write constraints, just an id, and then the referenced_ids of Flow are arranged

Change to 2 rows

 

 

Guess you like

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