【问题标题】:can ConstraintLayout help to flow the right side item to next line?ConstraintLayout 可以帮助将右侧项目流到下一行吗?
【发布时间】:2017-08-15 18:59:47
【问题描述】:

将 textView 点亮到左边缘,它的宽度可能会增加。让其他一些 textView 在其右对齐单行。

当左边textView宽度变大时,想把右边的textView下推到下一行:

[AAA]  [BBB] 

当左一宽度增加时:

[AAA AAA AAA AAA AAA]

[BBB]

想看到一些使用ConstraintLayout 自动将右侧项目向下推到下一行的示例,但找不到任何示例。

ContraintLayOut 是可行的,或者有人知道一些示例吗?

【问题讨论】:

标签: android textview android-constraintlayout flow


【解决方案1】:

更新:下面的答案仍然有效,但从 ConstraintLayout 2.0 开始,使用 Flow 帮助器可能会更好。有关示例,请参阅 this Stack Overflow 答案。


AFAIK,从一行到下一行的流动视图不是ConstraintLayout 可以做到的。然而,FlexboxLayout 可以很轻松地做到这一点。

Here 是对该技术的初步介绍。 Play 商店中也有一些不错的应用可以用来玩弄布局。

FlexboxLayout 可以解释为高级 LinearLayout,因为这两种布局都按顺序对齐其子视图。 LinearLayout 和 FlexboxLayout 之间的显着区别在于 FlexboxLayout 具有用于包装的功能。

【讨论】:

  • 感谢 Cheticamp 分享 FlexboxLayout!但真的很想知道如何使用 ConstrainLayout。
  • @lannyf 没问题。但是,我不认为你会得到你正在寻找的答案。 FlexboxLayout 在 ConstraintLayout 发布的同时也是开源的,并且因其包装视图的能力(您在 ConstraintLayout 中寻找的功能)而受到吹捧,这让我相信 ConstraintLayout 没有这种特殊能力。
  • 约束布局 2.0 现在有流助手。截至目前,它在 2.0.0-alpha1 中。
【解决方案2】:

你可以像这样使用com.google.android.material.chip.ChipGroup

<com.google.android.material.chip.ChipGroup
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_marginTop="8dp"
      android:layout_marginStart="8dp"
      android:layout_marginEnd="8dp"
      app:layout_constraintEnd_toEndOf="parent"
      app:layout_constraintStart_toStartOf="parent"
      app:layout_constraintTop_toBottomOf="@+id/title">
      <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="aaaaaaa"
        android:textSize="10sp"/>

      <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="bbbbbb"/>

      <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="cccccccc"/>

      <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="fffffffff"/>

      <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="hhhhhhh"/>
      <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="trtr"/>
    </com.google.android.material.chip.ChipGroup>

see more here

【讨论】:

    【解决方案3】:

    我知道我参加聚会已经很晚了,但是对于仍然订阅此问题的任何人,截至今天,ConstraintLayout 2.0(刚刚在 beta1 中推出)支持名为 Flow 的新 ConstraintHelper,使用您的特定要求可以在ConstraintLayout实现。

    Flow

    The following are the release notes for beta1

    【讨论】:

      【解决方案4】:

      搜了一圈,结论是ConstraintLayout是为了扁平化布局的视图层次,避免嵌套,优化视图层次。 所以正如@Cheticamp 指出的“ConstraintLayout 没有那种特殊的能力”。所以结束这个问题。

      【讨论】:

        猜你喜欢
        • 2017-07-31
        • 2011-05-01
        • 1970-01-01
        • 1970-01-01
        • 2018-09-28
        • 2023-03-25
        • 2021-01-16
        • 2021-09-23
        • 2021-01-28
        相关资源
        最近更新 更多