在 ConstraintLayout 中有一个具有相同宽度和高度的 ImageView

AVE*_*imi 5 imageview android-imageview android-constraintlayout

我们如何使 ImageView 的宽度和高度等于父 ConstraintLayout 的宽度?所以它显示一个全宽的方形 ImageView。

通常如何设置某个小部件的高度等于其他小部件的宽度?

Che*_*amp 8

以下布局将在 parent 的中心放置一个方形的蓝色图像ConstraintLayout。关键是app:layout_constraintDimensionRatio="1:1"。有关详细信息,请参阅文档

您还可以将小部件的一个维度定义为另一个维度的比率。为此,您需要将至少一个约束维度设置为 0dp(即 MATCH_CONSTRAINT),并将属性 layout_constraintDimensionRatio 设置为给定的比率。

<android.support.constraint.ConstraintLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@android:color/holo_blue_light"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintDimensionRatio="1:1"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)