【问题标题】:Android: Keep ratio for a full-width and undefined height ImageView in a ConstraintLayout?Android:在ConstraintLayout中保持全宽和未定义高度ImageView的比例?
【发布时间】:2019-04-07 19:07:09
【问题描述】:

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被扭曲了。

问题

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

【问题讨论】:

    标签: android android-imageview android-constraintlayout


    【解决方案1】:

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

    你的代码..

    android:background="@drawable/ic_background_stars"
    

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

    app:srcCompat="@drawable/ic_background_stars"
    

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

    【讨论】:

      【解决方案2】:

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

      例如,如果您想要正方形,则需要相同的宽度和高度,因此请使用 - app:layout_constraintDimensionRatio="1:1"

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-03-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多