首页 > 解决方案 > 如何在约束布局中将视图放置到与另一个视图的特定偏移量

问题描述

这张照片

所以,我有两张图片。一个(小)图像必须位于第二个图像(较小)的底部轴的中间,但应稍微偏离 Y 轴。我试图在约束布局中做到这一点。我知道将较小图像的顶部和底部约束设置为较大图像的底部。如何在约束布局中实现这一点? 编辑 1 我怎样才能将上述链接中的美女与野兽照片稍微向下移动到 Y 轴

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <ImageView
        android:id="@+id/singapore"
        android:scaleType="centerCrop"

        android:layout_width="240dp"
        android:layout_height="240dp"

        android:src="@drawable/singapore"
        app:layout_constraintEnd_toEndOf="parent"

        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

    </ImageView>

    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"

        android:src="@drawable/poster"

        app:layout_constraintBottom_toBottomOf="@id/singapore"
        app:layout_constraintEnd_toEndOf="@id/singapore"
        app:layout_constraintStart_toStartOf="@+id/singapore"
        app:layout_constraintTop_toBottomOf="@id/singapore">

    </ImageView>

</androidx.constraintlayout.widget.ConstraintLayout>

标签: androidxmlandroid-constraintlayout

解决方案


将较小图像的顶部和底部设置为较大图像的底部,将较小的图像垂直居中于较大图像的底部边缘。您现在可以10dp通过指定较小图像的平移来将较小的图像向下移动:

android:translationY="10dp"

请参阅查看文档中对 y 转换属性的描述。


推荐阅读