Android studio 在约束布局中将水平辅助对象放置在具有设置的自定义边距的视图底部

Android studio 在约束布局中将水平辅助对象放置在具有设置的自定义边距的视图底部,android-studio,android-layout,android-constraintlayout,Android Studio,Android Layout,Android Constraintlayout,如何设置它,以便将约束布局中的水平辅助对象设置在特定视图的底部,中间留有16dp 我所尝试的(使用横向指南): 它实际上并不关心layout\u marginTop和layout\u constraintTop\u toBottomOf属性。如果我删除layout\u constraintGuide\u percent属性,它会失控并粘在父对象的顶部 编辑 我创建了指南,因为我有一个视图,它在多个视图后面充当“背景”。我希望将此背景视图约束到指南的底部,与上次垂直对齐的视图(从多个视图)的偏

如何设置它,以便将
约束布局
中的水平辅助对象设置在特定
视图
的底部,中间留有
16dp

我所尝试的(使用横向
指南
):


它实际上并不关心
layout\u marginTop
layout\u constraintTop\u toBottomOf
属性。如果我删除
layout\u constraintGuide\u percent
属性,它会失控并粘在父对象的顶部

编辑

我创建了
指南
,因为我有一个
视图
,它在多个
视图
后面充当“背景”。我希望将此背景
视图
约束到
指南的底部
,与上次垂直对齐的
视图
(从多个
视图
)的偏移量为16dp。我希望此背景
视图
的高度基于多个
视图
中最后一个垂直对齐的
视图
,即高度由视图底部+16dp边距确定。如果我将所述背景
视图的底部约束到最后一个垂直对齐的
视图的底部,并添加一个底部边距,背景
视图
从最后一个垂直对齐的
视图的底部缩回16dp

我将使用高度为
16dp
且顶部约束在最低视图底部的。可以将背景视图的底部约束到空间视图的底部,以便使用
64dp
来突出边距,以如下方式直观地创建边距:

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

    <View
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@android:color/holo_red_light"
        app:layout_constraintBottom_toBottomOf="@id/space"
        app:layout_constraintEnd_toEndOf="@id/view1"
        app:layout_constraintStart_toStartOf="@id/view1"
        app:layout_constraintTop_toTopOf="@+id/view1" />

    <TextView
        android:id="@+id/view1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:text="Using a Space widget"
        android:textColor="@android:color/white"
        android:textSize="30sp"
        app:layout_constraintBottom_toTopOf="@+id/view2"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_chainStyle="packed" />

    <TextView
        android:id="@+id/view2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="view2"
        android:textColor="@android:color/white"
        android:textSize="30sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/view1" />

    <Space
        android:id="@+id/space"
        android:layout_width="wrap_content"
        android:layout_height="64dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/view2" />

</androidx.constraintlayout.widget.ConstraintLayout>

我会使用高度为
16dp
且顶部限制在最低视图底部的。可以将背景视图的底部约束到空间视图的底部,以便使用
64dp
来突出边距,以如下方式直观地创建边距:

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

    <View
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@android:color/holo_red_light"
        app:layout_constraintBottom_toBottomOf="@id/space"
        app:layout_constraintEnd_toEndOf="@id/view1"
        app:layout_constraintStart_toStartOf="@id/view1"
        app:layout_constraintTop_toTopOf="@+id/view1" />

    <TextView
        android:id="@+id/view1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:text="Using a Space widget"
        android:textColor="@android:color/white"
        android:textSize="30sp"
        app:layout_constraintBottom_toTopOf="@+id/view2"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_chainStyle="packed" />

    <TextView
        android:id="@+id/view2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="view2"
        android:textColor="@android:color/white"
        android:textSize="30sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/view1" />

    <Space
        android:id="@+id/space"
        android:layout_width="wrap_content"
        android:layout_height="64dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/view2" />

</androidx.constraintlayout.widget.ConstraintLayout>

您看到的是正确的行为。助手通常不遵守边距。指南的放置基于百分比或固定距离。你需要这样的指引吗?为什么不用16dp的边距限制到视图的底部呢?@Cheticamp我明白了。是的,我确实需要这个指南,因为我有一个
视图
,它在多个
视图
后面充当“背景”。我希望此背景
视图
的高度基于多个
视图
中最后一个垂直对齐的
视图
,即高度由视图底部+16dp边距确定。如果我将所述背景
视图的底部约束到最后一个垂直对齐的
视图的底部,并添加底部边距,则背景
视图将从最后一个垂直对齐的
视图的底部缩回16dp,这是有意义的。使用ConstraintLayout 2.0,您可以使用设置为视图的背景,并将底部填充设置为
16dp
。您不能将其他视图约束到此层小部件,但是,如果它只是一个背景,那就无关紧要了。否则,您可以放置顶部边距为
16dp
的空间小部件,并将其顶部约束到最底部视图的底部。然后可以将背景视图约束到空间视图的顶部,这将为您提供
16dp
边距。@Cheticamp哇,我不知道图层小部件。但是我昨天做了更多的研究,发现了
空间
小部件。您想在下面提供完整的答案吗(可能还有实现)?您看到的是正确的行为。助手通常不遵守边距。指南的放置基于百分比或固定距离。你需要这样的指引吗?为什么不用16dp的边距限制到视图的底部呢?@Cheticamp我明白了。是的,我确实需要这个指南,因为我有一个
视图
,它在多个
视图
后面充当“背景”。我希望此背景
视图
的高度基于多个
视图
中最后一个垂直对齐的
视图
,即高度由视图底部+16dp边距确定。如果我将所述背景
视图的底部约束到最后一个垂直对齐的
视图的底部,并添加底部边距,则背景
视图将从最后一个垂直对齐的
视图的底部缩回16dp,这是有意义的。使用ConstraintLayout 2.0,您可以使用设置为视图的背景,并将底部填充设置为
16dp
。您不能将其他视图约束到此层小部件,但是,如果它只是一个背景,那就无关紧要了。否则,您可以放置顶部边距为
16dp
的空间小部件,并将其顶部约束到最底部视图的底部。然后可以将背景视图约束到空间视图的顶部,这将为您提供
16dp
边距。@Cheticamp哇,我不知道图层小部件。但是我昨天做了更多的研究,发现了
空间
小部件。您是否愿意在下面提供完整的答案(可能还有实现)?谢谢您的回答。我已经接受了你的回答。谢谢你的回答。我已经接受了你的回答。
<androidx.constraintlayout.widget.ConstraintLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.constraintlayout.helper.widget.Layer
        android:id="@+id/layer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/holo_red_light"
        android:paddingBottom="64dp"
        app:constraint_referenced_ids="view2,view1"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/view1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Using a Layer"
        app:layout_constraintVertical_chainStyle="packed"
        android:textColor="@android:color/white"
        android:textSize="30sp"
        android:layout_marginBottom="16dp"
        app:layout_constraintBottom_toTopOf="@+id/view2"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/view2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="view2"
        android:textColor="@android:color/white"
        android:textSize="30sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/view1" />

</androidx.constraintlayout.widget.ConstraintLayout>