在 ConstraintLayout 中有一个具有相同宽度和高度的 ImageView
Have an ImageView with same width and height in ConstraintLayout
如何让ImageView的宽度和高度等于父ConstraintLayout的宽度?所以它显示一个全宽的正方形 ImageView。
一般如何设置一些小部件的高度等于其他小部件的宽度?
以下布局将在父级 ConstraintLayout
的中心放置一个方形蓝色图像。关键部分是app:layout_constraintDimensionRatio="1:1"
。详见documentation。
You can also define one dimension of a widget as a ratio of the other one. In order to do that, you need to have at least one constrained dimension be set to 0dp (i.e., MATCH_CONSTRAINT), and set the attribute layout_constraintDimensionRatio to a given ratio.
<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>
如何让ImageView的宽度和高度等于父ConstraintLayout的宽度?所以它显示一个全宽的正方形 ImageView。
一般如何设置一些小部件的高度等于其他小部件的宽度?
以下布局将在父级 ConstraintLayout
的中心放置一个方形蓝色图像。关键部分是app:layout_constraintDimensionRatio="1:1"
。详见documentation。
You can also define one dimension of a widget as a ratio of the other one. In order to do that, you need to have at least one constrained dimension be set to 0dp (i.e., MATCH_CONSTRAINT), and set the attribute layout_constraintDimensionRatio to a given ratio.
<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>