Android:在ConstraintLayout中保持全宽和未定义高度ImageView的比率?

问题描述 投票:1回答:2

ConstraintLayout中,ImageView与其父母绑定在一起:

  1. 它的左侧绑定在屏幕的左侧
  2. 它的右侧与屏幕的右侧相连
  3. 它的顶端绑定到小部件的底部
  4. 它的底边绑定在屏幕的底部

因此,我的ImageView似乎是全宽,其高度是小部件和屏幕底部之间的距离。

<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是distorded。

Question

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

android android-imageview android-constraintlayout
2个回答
1
投票

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

你的代码..

android:background="@drawable/ic_background_stars"

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

app:srcCompat="@drawable/ic_background_stars"

ImageView的默认缩放类型应该为您完成工作。此外,您甚至可以尝试各种比例类型。


1
投票

我认为如果您的图像,您可以使用app:layout_constraintDimensionRatio="your ratio"来保持纵横比

例如,如果你想要方形,你需要相同的宽度和高度,所以使用 - app:layout_constraintDimensionRatio="1:1"

© www.soinside.com 2019 - 2024. All rights reserved.