我们如何使 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,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52569240/