android - ConstraintLayout 流程助手示例

标签 android xml layout android-constraintlayout

我在 blog entry 中读到,在 ConstraintLayout 2.0.0 中,引入了一种创建 View 流的方法。我真正想要实现的目标: 我有几个固定大小的 TextView 彼此相邻。根据屏幕大小,一些 TextView 应该被推到下一行。

带有六个 TextViews 的大屏幕示例:

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

[EEE] [FFF]

带有六个 TextViews 的小屏幕示例:

[AAA] [BBB]

[CCC] [DDD]

[EEE] [FFF]

我已经看到 this Stackoverflow 问题建议使用 FlexboxLayout,但有评论说现在可以使用 ConstraintLayout 来实现同样的事情。

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

最佳答案

您可以通过在 androidx.constraintlayout.widget.ConstraintLayout 中添加一个 androidx.constraintlayout.helper.widget.Flow 虚拟 View 并引用所有 TextView 来实现此目的带有 app:constraint_referenced_ids 属性。

下面的例子展示了我是如何做到的。

<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_ 属性来更改 TextView 的排列,例如对齐方式、边距等。 https://developer.android.com/reference/androidx/constraintlayout/helper/widget/Flow

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

Result

关于android - ConstraintLayout 流程助手示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54874011/

相关文章:

xml - 使用 CacheStore、JDBC 驱动程序和 xml 配置部署 Ignite 应用程序的最佳选择

jquery - 使用 HTML 和 CSS 的半砖图像布局

java - 无法为RatingBar设置评级

Android - 应用程序最小化处理

android - 如何减少 Android 相机快门延迟的可变性?

android - RecyclerView 项目 wrap_content 宽度

html - 似乎无法让图像、文本等显示在我的垂直导航菜单旁边

Camera.open() 不工作安卓

c# - 获取带有属性的 XmlNode 打开标记

android - Universal-Image-Loader 不遵循 layout_weight