ConstraintLayout Flow帮助程序示例 [英] ConstraintLayout Flow helper example

查看:99
本文介绍了ConstraintLayout Flow帮助程序示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了博客条目ConstraintLayout 2.0.0中,引入了一种创建视图流的方法.我确切想要实现的目标: 我有几个彼此固定大小的TextView.根据屏幕尺寸,一些TextView应该推到下一行.

I have read in a blog entry that in ConstraintLayout 2.0.0, a way was introduced to create a flow of views. What I exactly want to achieve: I have several TextViews with a fixed size next to each other. Depending on screen size, some TextViews should be pushed into the next line.

在具有六个TextViews的大屏幕上的示例:

Example on big screen with six TextViews:

[AAA] [BBB] [CCC] [DDD]

[AAA] [BBB] [CCC] [DDD]

[EEE] [FFF]

[EEE] [FFF]

在具有六个TextViews的小屏幕上的示例:

Example on small screen with six TextViews:

[AAA] [BBB]

[AAA] [BBB]

[CCC] [DDD]

[CCC] [DDD]

[EEE] [FFF]

[EEE] [FFF]

我已经看到建议使用FlexboxLayout的Stackoverflow问题,但有一条评论说,现在可以使用ConstraintLayout来实现相同的目的.

I already saw this Stackoverflow question proposing to use a FlexboxLayout, but there is a comment saying that the same thing now can be achieved using ConstraintLayout.

任何人都可以给我一个例子,说明如何使用ConstraintLayout实现所需的行为?我找不到关于此的任何说明.预先感谢.

Anybody can give me an example on how to achieve the desired behavior using ConstraintLayout? I was not able to find any instructions about this. Thanks in advance.

推荐答案

您可以通过在androidx.constraintlayout.widget.ConstraintLayout内添加androidx.constraintlayout.helper.widget.Flow虚拟视图并使用app:constraint_referenced_ids属性引用所有文本视图来实现此目的.

You can achieve this by adding a androidx.constraintlayout.helper.widget.Flow virtual view inside the androidx.constraintlayout.widget.ConstraintLayout and by referencing all your textviews with app:constraint_referenced_ids attribute.

下面的示例显示了我是如何实现的.

Example below shows how I've achieved it.

<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"
    android:orientation="vertical">

    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="[AAAAAAAA]"
        tools:ignore="MissingConstraints" />

    <TextView
        android:id="@+id/text2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="[BBBBBBBB]"
        tools:ignore="MissingConstraints" />

    <TextView
        android:id="@+id/text3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="[CCCCCCCC]"
        tools:ignore="MissingConstraints" />

    <TextView
        android:id="@+id/text4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="[DDDDDDDD]"
        tools:ignore="MissingConstraints" />

    <TextView
        android:id="@+id/text5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="[EEEEEEEE]"
        tools:ignore="MissingConstraints" />

    <TextView
        android:id="@+id/text6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="[FFFFFFFF]"
        tools:ignore="MissingConstraints" />

    <androidx.constraintlayout.helper.widget.Flow
        android:id="@+id/flow1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:padding="10dp"
        app:constraint_referenced_ids="text1,text2,text3,text4,text5,text6"
        app:flow_horizontalBias="0"
        app:flow_horizontalGap="10dp"
        app:flow_horizontalStyle="packed"
        app:flow_verticalBias="0"
        app:flow_wrapMode="chain"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

使用app:flow_属性来改变文本视图的排列方式,例如对齐方式,边距等. https://developer.android.com/reference/androidx/constraintlayout/helper/widget/Flow

Play around with app:flow_ attributes to change the arrangement of the textviews such as alignment, margin, etc. https://developer.android.com/reference/androidx/constraintlayout/helper/widget/Flow

最终结果应如下所示,具体取决于您的屏幕尺寸.

Final result should look like below depending on your screen size.

这篇关于ConstraintLayout Flow帮助程序示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆