【问题标题】:how can I constraint a view to the center of another views bottom?如何将视图约束到另一个视图底部的中心?
【发布时间】:2019-05-20 00:04:21
【问题描述】:

我想让一张图片始终完美地居中于另一张图片的底部。

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

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

*我需要为前景图像保留一个比例。

【问题讨论】:

  • 你能张贴一张形象化你想要达到的目标的图片吗?

标签: android android-layout android-constraintlayout


【解决方案1】:

您可以通过将您的视图限制在目标的开始和结束来做到这一点,这样它就会保持在目标的中心。这是一个示例:

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

【讨论】:

  • 谢谢威廉,但这不是我需要的。我已附上正确答案,如果您有兴趣,可以查看我的实际问题以及解决方案是什么
【解决方案2】:

我在这里找到了答案:

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

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

【讨论】:

    【解决方案3】:

    我想这就是你要找的。​​p>

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

    【讨论】:

      猜你喜欢
      • 2021-06-28
      • 2021-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多