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

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

相关文章:

android - 键盘出现时隐藏ImageView,键盘消失时显示

android - 使用 Glide android 分别加载 2 张图片

android - 尝试旋转图像时程序停止

android - 设置后让ImageView消失

ios - 带有 iOS 应用程序图像查看器的 ImageSlider

android - 为什么我的带 alpha 的 ImageView 放在不透明的 ImageView 上时不可见?

java - 如何在保持宽高比的同时填充应用程序小部件中的 ImageView?

android - ConstraintLayout 中的边距如何工作?

android - 具有约束布局的自定义类,Android Studio 挂起