【发布时间】:2019-03-05 06:32:12
【问题描述】:
如何让ImageView的宽高等于父ConstraintLayout的宽度?所以它会显示一个全宽的方形 ImageView。
通常如何将某个小部件的高度设置为等于其他小部件的宽度?
【问题讨论】:
标签: imageview android-imageview android-constraintlayout
如何让ImageView的宽高等于父ConstraintLayout的宽度?所以它会显示一个全宽的方形 ImageView。
通常如何将某个小部件的高度设置为等于其他小部件的宽度?
【问题讨论】:
标签: imageview android-imageview android-constraintlayout
以下布局将在父ConstraintLayout 的中心放置一个方形蓝色图像。关键是app:layout_constraintDimensionRatio="1:1"。详情请参考documentation。
您还可以将小部件的一个维度定义为另一个维度的比率。为此,您需要将至少一个约束维度设置为 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>
【讨论】: