首页 > 解决方案 > Android:在ConstraintLayout中保持全宽和未定义高度ImageView的比例?

问题描述

在 aConstraintLayout中,an 以ImageView如下方式绑定到其父级:

  1. 它的左侧绑定到屏幕的左侧

  2. 它的右侧绑定到屏幕的右侧

  3. 它的顶部绑定到小部件的底部

  4. 它的底边绑定到屏幕的底边

因此 myImageView似乎是全角的,它的高度是小部件与屏幕底部之间的距离。

<ImageView
    android:id="@+id/imageView16"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:orientation="horizontal"
    android:background="@drawable/ic_background_stars"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toBottomOf="@id/linearLayout4"
    app:layout_constraintBottom_toBottomOf="parent"
    />

问题是它包含的 SVG 也是全宽的,但比例已被破坏:就高度而言,它不够大。所以SVG是扭曲的。

问题

如何以全宽显示 SVG(从屏幕左侧到右侧),同时保持其比例(使其高度足够大)?

标签: androidandroid-imageviewandroid-constraintlayout

解决方案


除了最后一个答案之外,您的图像未正确缩放的原因是因为您将其用作背景。

你的代码..

android:background="@drawable/ic_background_stars"

相反,将其用作图像资源。

app:srcCompat="@drawable/ic_background_stars"

ImageView 的默认比例类型应该为您完成工作。否则,您甚至可以尝试各种规模类型。


推荐阅读