如何为约束布局组提供垂直边距?

How to give vertical margin to constraint layout group?

所以我有一些观点,我使用约束布局组对其进行分组。如果我使用 linear/relative 布局对其进行分组,我想为该组提供垂直边距。添加 android:layout_marginVertical="100dp" 似乎不起作用。

您尝试执行的操作不正确。

ConstraintLayout Group可以用来对不同视图或widgets的一些引用进行“分组”,并设置它们的可见性(你可以设置组的可见性,所有视图都会得到)。它不是像 LinearLayout 那样的财务组。

请看一下文档

https://developer.android.com/reference/androidx/constraintlayout/widget/Group

This class controls the visibility of a set of referenced widgets. The visibility of the group will be applied to the referenced widgets. It's a convenient way to easily hide/show a set of widgets without having to maintain this set programmatically.

对于您想要做的事情,您应该使用布局(LinearLayout、ConstraintLayout...)并将视图分组在布局内,最后为该布局提供所需的边距。

群组不是那样工作的。看一下自 2.0 版以来可用的 ConstraintLayout 的 Layer widgetLayer的使用教程可以搜索一下。简而言之,Layer 将允许您对类似于 ViewGroup 的小部件进行分组,但会保持平面布局结构。如果层中包含的视图可以约束到层本身,则整个层将接受上边距。

例如,采用以下布局:

蓝色是图层的背景。 top/bottom、right/left 视图包含在图层中并受其约束。 100dp 的上边距设置在 Layer 上。 “外部视图”是一个 TextView,它被限制在 Layer[=] 中的“右下角”TextVIew 36=] 如果将 Layer 替换为 ViewGroup.

则无法完成的事情

视图可能仍包含在 中以进行组操作。

这是此布局的 XML:

<androidx.constraintlayout.widget.ConstraintLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <androidx.constraintlayout.helper.widget.Layer
        android:id="@+id/layer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="100dp"
        android:background="@android:color/holo_blue_light"
        app:constraint_referenced_ids="topLeft,topRight,bottomLeft,bottomRight"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/topLeft"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="8dp"
        android:text="Top left"
        android:textAppearance="@style/TextAppearance.AppCompat.Display1"
        app:layout_constraintStart_toStartOf="@id/layer"
        app:layout_constraintTop_toTopOf="@id/layer" />

    <TextView
        android:id="@+id/topRight"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="8dp"
        android:text="Top right"
        android:textAppearance="@style/TextAppearance.AppCompat.Display1"
        app:layout_constraintStart_toStartOf="@+id/bottomRight"
        app:layout_constraintTop_toTopOf="@id/layer" />

    <TextView
        android:id="@+id/bottomLeft"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="8dp"
        android:text="Bottom left"
        android:textAppearance="@style/TextAppearance.AppCompat.Display1"
        app:layout_constraintStart_toStartOf="@id/layer"
        app:layout_constraintTop_toBottomOf="@+id/topLeft" />

    <TextView
        android:id="@+id/bottomRight"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="8dp"
        android:text="Bottom right"
        android:textAppearance="@style/TextAppearance.AppCompat.Display1"
        app:layout_constraintStart_toEndOf="@+id/bottomLeft"
        app:layout_constraintTop_toBottomOf="@+id/topRight" />

    <TextView
        android:id="@+id/outsideView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="Outside view"
        android:textAppearance="@style/TextAppearance.AppCompat.Display1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/bottomRight" />

</androidx.constraintlayout.widget.ConstraintLayout>