Android ConstraintLayout - 如何创建不均匀的链space?

Android ConstraintLayout - how to create chain with uneven space?

我如何使用 ConstraintLayouts 创建它? 我想在右侧附加一些视图,在左侧附加一些视图,在中间附加变量 space,这取决于 right/left 上视图的长度。我知道链,但它们总是在视图之间添加偶数 space,我不想给 space 一个固定值。在线性布局中,我会做这样的事情:

<LinearLayout >
<ImageView />
<TextView />

<Space
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1" />

<ImageView />
<ImageView />
<TextView />
<LinearLayout/>

这可以在 ConstraintLayout 中复制吗?

您可以像这样使用 ConstraintLayout 实现此目的:

<ConstraintLayout >

    <LinearLayout
        android:id="@+id/first"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:gravity="end"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/first"
        app:layout_constraintTop_toTopOf="parent"/>

</ConstraintLayout>

当您可以直接对单个元素使用 Constraints 时,为什么需要链或中间的 Space。看到这个伪代码,这可以帮助。

<androidx.constraintlayout.widget.ConstraintLayout
  .....
  .....
>
<TextView
  .....
  app:layout_constraintStart_toStartOf="parent"
  app:layout_constraintTop_toTopOf="parent"
  android:layout_marginStart="10dp"
  android:drawableStart=" Your clock icon" //Using this you don't have to use a separate ImageView
 />
 <ImageView
  .....
  app:layout_constraintEnd_toStartOf=" ID of your Download Count TextView"
  app:layout_constraintTop_toTopOf="parent"
  android:layout_marginEnd="10dp"
 />
 <TextView
  .....
  app:layout_constraintEnd_toEndOf="parent"
  app:layout_constraintTop_toTopOf="parent"
  android:layout_marginEnd="10dp"
  android:drawableStart=" Your Download icon"
 />
 </androidx.constraintlayout.widget.ConstraintLayout>

我使用了 drawbleStart 而不是单独的 ImageView,如果您愿意,可以使用 app:layout_constraintEnd_toStartOf=" ID of the View"。给定的边距是屏幕end/start和视图之间的space,你可以随意调整。

这将为您提供您共享的视图。检查这是否有帮助。