如何解决如何给约束布局组垂直边距?
因此,我有一些视图可以使用约束布局组对其进行分组。如果要使用线性/相对布局对它进行分组,我想像给垂直边距一样。添加android:layout_marginVertical="100dp"
似乎无效。
解决方法
组不能那样工作。看看自2.0版以来可用的ConstraintLayout的Layer widget。您可以搜索有关使用 Layer 的教程。简而言之, Layer 将允许您将类似于 ViewGroup 的小部件分组,但将保持平面布局结构。如果可以将包含在该图层中的视图限制为该图层本身,则整个图层都将接受顶部空白。
例如,采用以下布局:
蓝色是图层的背景。顶视图/底视图,右视图/左视图包含在该层中,并受其约束。 100dp
的上边距是在 Layer 上设置的。 “外部视图”是一个 TextView ,它被限制在 Layer 中包含的“右下” TextVIew 如果将 Layer 替换为 ViewGroup 。
视图仍可能包含在 Group 中,以进行组操作。
以下是此布局的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>
,
您尝试做的事不正确。
ConstraintLayout组可用于将不同视图或窗口小部件的某些引用“分组”,并设置其可见性(您可以设置组的可见性,所有视图均会得到它)。这不是一个像LinearLayout那样可以成立的组织。
请查看文档
https://developer.android.com/reference/androidx/constraintlayout/widget/Group
此类控制一组引用的窗口小部件的可见性。 该组的可见性将应用于引用的窗口小部件。这是一种方便的方式,可以轻松地隐藏/显示一组小部件,而无需以编程方式对其进行维护。
对于您想做的事情,您应该使用Layout(LinearLayout,ConstraintLayout ...)并将该视图内部的组进行分组,最后为该布局提供所需的边距。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。