繁体   English   中英

如何将视图约束到另一个视图底部的中心?

[英]how can I constraint a view to the center of another views bottom?

我希望图像始终完美地位于另一个图像底部的顶部。

我的第一个想法是将背景图像的底部约束设置为,例如从另一个视图设置为100dp,然后将前景图像的底部约束设置为来自同一视图的50dp,并将其高度设置为100dp。 这样它总是位于背景图像底部中心的中心,但是我通过给它一个固定的大小来使前景视图没有响应,这是我想要避免的。

我的另一个想法是将前景图像的顶部和底部约束到背景图像的底部,但随后视图就会折叠。 我找不到任何可以让它工作的设置。

*我需要保持前景图像的比例。

您可以通过将视图约束到目标的开始和结束来实现,以便它保持居中。 这是一个示例:

<View
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintTop_toBottomOf="@+id/targetView"
    app:layout_constraintStart_toStartOf="@+id/targetView"
    app:layout_constraintEnd_toEndOf="@+id/targetView" />

我在这里找到答案:

https://gist.github.com/writtmeyer/f5971266394b2316eb01b566cdeff2a0

如果使用虚拟视图,它建议了一种聪明的方法。

我想这就是你要找的东西。

<ImageView
    android:id="@+id/backgroundImage"
    android:layout_width="150dp"
    android:layout_height="150dp"
    app:srcCompat="@mipmap/ic_launcher" />

<ImageView
    android:id="@+id/foregroundImage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintBottom_toBottomOf="@id/backgroundImage"
    app:layout_constraintEnd_toEndOf="@id/backgroundImage"
    app:layout_constraintStart_toStartOf="@id/backgroundImage"
    app:layout_constraintTop_toTopOf="@id/backgroundImage"
    app:srcCompat="@mipmap/ic_launcher"/>

暂无
暂无

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

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