美文网首页每天一个秃头小技巧
【Android】约束布局ConstraintLayout建立一

【Android】约束布局ConstraintLayout建立一

作者: littlefogcat | 来源:发表于2024-12-19 10:04 被阅读0次

使用 app:layout_constraintDimensionRatio 属性可以约束控件的宽高比例。
例如,我想要显示一个正方形的图片,则使用以下布局:

    <ImageView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:scaleType="centerCrop"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintDimensionRatio="1:1"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

以上代码中,约束了控件上、左、下边界与父布局重合,并且宽度与高度的比例为1:1。

相关文章

网友评论

    本文标题:【Android】约束布局ConstraintLayout建立一

    本文链接:https://www.haomeiwen.com/subject/uytjejtx.html