简体   繁体   English

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

[英]How to give vertical margin to constraint layout group?

So I have a few view that I group it using constraint layout group.所以我有一些视图,我使用约束布局组对其进行分组。 I want to give vertical margin to the group as I do if I group it using linear/relative layout.如果我使用线性/相对布局对其进行分组,我想为该组提供垂直边距。 add android:layout_marginVertical="100dp" doesn't seem to work.添加android:layout_marginVertical="100dp"似乎不起作用。

Groups don't work like that.团体不是那样工作的。 Take a look at ConstraintLayout's Layer widget available since version 2.0.看看自 2.0 版以来可用的 ConstraintLayout 的图层小部件 You can search for tutorials on the use of Layer .您可以搜索有关使用Layer 的教程。 In a nutshell, Layer will allow you to group widgets similar to a ViewGroup but will maintain a flat layout structure.简而言之, Layer将允许您对类似于ViewGroup 的小部件进行分组,但将保持平面布局结构。 If the views contained with the layer can be constrained to the layer itself, the whole layer will accept a top margin.如果图层中包含的视图可以被约束到图层本身,则整个图层将接受一个上边距。

For example, take the following layout:例如,采用以下布局:

在此处输入图片说明

The blue is the background for the layer.蓝色是图层的背景。 The top/bottom, right/left views are contained within the layer and are constrained to it.顶/底、右/左视图包含在图层内并受其约束。 The top margin of 100dp is set on the Layer .Layer上设置了100dp的上边距。 "Outside view" is a TextView that is constrained to the "Bottom right" TextVIew which is contained within the Layer something that can't be done if Layer is replaced by a ViewGroup . “外部视图”是一个TextView ,它被限制在“右下角” TextVIew 中,它包含在Layer 中,如果LayerViewGroup替换,则无法完成。

The views may still be contained within a Group for group manipulations.视图可能仍包含在组中以进行组操作。

Here is the XML for this layout:这是此布局的 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>

what you are trying to do is not correct.你试图做的是不正确的。

ConstraintLayout Group can be used to "group" some references of different views or widgets and set their visibility (you can set the visibility of the group and all views will get it). ConstraintLayout Group 可用于“分组”不同视图或小部件的一些引用并设置它们的可见性(您可以设置组的可见性,所有视图都会得到它)。 It's not a fisical group like a LinearLayout can make.它不是像 LinearLayout 那样的财政组。

Please, take a look at the documentation请看一下文档

https://developer.android.com/reference/androidx/constraintlayout/widget/Group 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.这是一种轻松隐藏/显示一组小部件而无需以编程方式维护该组的便捷方式。

For what you want to do, you should use a Layout (LinearLayout, ConstraintLayout...) and group that views inside the layout and finally give the desired margin to this layout.对于您想要做的事情,您应该使用布局(LinearLayout、ConstraintLayout...)并将该视图分组在布局内,最后为该布局提供所需的边距。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM