Android ConstraintLayout 约束布局 Flow 流式布局,表格布局

Android四大布局中的表格布局。只有在面试的时候用上。

那么约束布局如何是来替代他的呢

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

核心就是其他的布局不要写约束条件 就一个id就好 然后Flow的referenced_ids 给安排上

改成2排

猜你喜欢

转载自blog.csdn.net/mp624183768/article/details/124458608