将视图的底部与 ConstraintLayout 的中心对齐

Align bottom of a view to the center of ConstraintLayout

我有一个自定义视图,我想将其放置在 ConstraintLayout 中,使其底部位于 ConstraintLayout 的中心。

子视图具有 wrap_content 的动态高度,因此我无法轻松设置固定边距或填充。

我知道我可以使用

将视图的中心与 ConstraintLayout 的中心对齐
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"

但正如我所说,我想将视图的底部与 ConstraintLayout 的中心对齐。

有什么好的方法可以做到这一点?

您可以使用 Guideline 为您的布局添加额外的约束,如下所示:

<android.support.constraint.Guideline
    android:id="@+id/guideline"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    app:layout_constraintGuide_percent="0.5"/>

,然后调整你的观点与这个准则是这样的:

    app:layout_constraintBottom_toTopOf="@+id/guideline"