【问题标题】:Placing a horizontal helper in Constraint Layout to be at the bottom of a View with a set custom margin在约束布局中放置一个水平助手以设置自定义边距的视图底部
【发布时间】:2019-11-26 11:13:44
【问题描述】:

如何设置它,以便将Constraint Layout 中的水平助手设置在特定View 的底部,其间的边距为16dp

我试过的(水平Guideline):

<androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.30779755"
        app:layout_constraintTop_toBottomOf="@id/view2" />

它实际上并不关心layout_marginToplayout_constraintTop_toBottomOf 属性。如果我删除 layout_constraintGuide_percent 属性,它会变得混乱并粘在父级的顶部。

编辑

我创建了 Guideline,因为我有一个 View,它充当 multiple Views 背后的“背景”。我希望将这个背景View 限制在这个Guideline 的底部,与最后一个垂直对齐的View(来自多个Views)有16dp 的偏移。我希望这个背景View 的高度基于多个Views 中最后一个垂直对齐的View,即高度由视图底部+ 16dp 边距确定。如果我将所述背景View 的底部限制到最后一个垂直对齐的View 的底部并添加底部边距,则背景View 会从最后一个垂直对齐的View 的底部缩回16dp。

【问题讨论】:

  • 您看到的是正确的行为。助手通常不遵守边距。指南的放置基于百分比或固定距离。你需要这样的指南吗?为什么不用 16dp 的边距限制到视图的底部?
  • @Cheticamp 我明白了。是的,我确实需要该指南,因为我有一个 View,它充当 multiple Views 后面的“背景”。我希望这个背景View 的高度基于多个Views 中最后一个垂直对齐的View,即高度由视图底部+ 16dp 边距确定。如果我将所述背景View 的底部约束到最后一个垂直对齐View 的底部并添加底部边距,则背景View 反而从最后一个垂直对齐View 的底部缩回16dp。
  • 有道理。使用 ConstraintLayout 2.0,您可以使用 Layer widget 为视图设置背景,并将底部填充设置为 16dp。您不能将其他视图约束到此图层小部件,但如果它只是一个背景,那没关系。否则,您可以放置​​一个上边距为16dp 的空间小部件,并将其顶部限制在最底部视图的底部。然后可以将背景视图限制在空间视图的顶部,这将为您提供16dp 边距。
  • @Cheticamp 哇,我不知道图层小部件。但我昨天做了更多研究,发现了Space 小部件。您想在下面提供完整的答案吗(也许还有实现)?

标签: android-studio android-layout android-constraintlayout


【解决方案1】:

我会使用高度为16dpSpace widget,并且顶部限制在最低视图的底部。可以将背景视图的底部限制在 Space 视图的底部,以直观地创建边距,如下所示,使用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>

我还提到了使用底部填充为16dpLayer helper。虽然这目前适用于 ConstraintLayout 2.0.0-beta3,但我不相信它应该有效,因此请自行决定使用。这是使用64dp 填充以突出边距的 再次显示的样子。您将需要 ConstraintLayout 2.0 或更高版本。

<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>

【讨论】:

  • 感谢您的回答。我接受了你的回答。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-04-29
  • 1970-01-01
  • 1970-01-01
  • 2012-02-12
  • 1970-01-01
  • 2018-11-26
  • 2018-11-20
相关资源
最近更新 更多