Android:在ConstraintLayout中保持全角和未定义高度ImageView的比例吗? [英] Android: Keep ratio for a full-width and undefined height ImageView in a ConstraintLayout?

查看:110
本文介绍了Android:在ConstraintLayout中保持全角和未定义高度ImageView的比例吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ConstraintLayout中,ImageView与其父级绑定的方式如下:

In a ConstraintLayout, an ImageView is bound to its parent in such a way that:

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

  1. Its left side is bound to the screen's left side

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

Its right side is bound to the screen's right side

其顶部绑定到小部件的底部

Its top side is bound to a widget's bottom side

它的底部绑定到屏幕的底部

Its bottom side is bound to the screen's bottom side

因此,我的ImageView似乎是全角,其高度是小部件与屏幕底部之间的距离.

Thus my ImageView appears to be full-width, and its height is the distance between a widget and the bottom of the screen.

<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失真了.

The problem is that the SVG it contains is also full-width, but the ratio has been destroyed: it's not big enough, in terms of height. So the SVG is distorded.

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

推荐答案

除了最后一个答案,图像未正确缩放的原因还在于您将其用作背景.

In addition to the last answer, the reason your image is not being scaled properly is because you are using it as the background.

您的代码.

android:background="@drawable/ic_background_stars"

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

Instead, use it as image resource.

app:srcCompat="@drawable/ic_background_stars"

并且ImageView的默认比例类型应为您完成工作.否则,您甚至可以尝试各种比例尺.

And the default scale type of ImageView should do the work for you. Else, you can even experiment with various scale types.

这篇关于Android:在ConstraintLayout中保持全角和未定义高度ImageView的比例吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆