gpt4 book ai didi

android - 将 ImageView 大小与 ConstrainLayout 的比例用于响应性

转载 作者:行者123 更新时间:2023-11-29 00:03:01 25 4
gpt4 key购买 nike

我正在使用 ConstraintLayout 使我的应用响应不同的屏幕尺寸。

我想使用比率来调整我的小部件的大小。所以要使用比率,我必须将宽度和高度设置为 match_constraints。

所以我正在做的是:

    <ImageView
android:id="@+id/my_img"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="150dp"
android:layout_marginStart="250dp"
android:layout_marginTop="5dp"
android:adjustViewBounds="true"
android:src="@drawable/img_test"
app:layout_constraintBottom_toTopOf="@+id/bot_img"
app:layout_constraintDimensionRatio="h,125:50"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/top_img" />

所以我将使用边距和比率(以 dp 为单位)来设置我的 img 大小。问题是当我使用不同的屏幕尺寸时,我的 img 不会保持我想要的精确比例。

你能帮帮我吗?我使用 ConstraintLayout 对吗?非常感谢

最佳答案

我认为您误解了 constraintDimensionRatio 的用法。使用 width="0dp" 设置两个水平约束以及设置 constraintDimensionRatio(如 1:1)。并使用 height="0dp" 仅设置一个垂直约束(如顶部)并保留另一个(或反之亦然,具体取决于您的要求)。现在您的 View 的宽度将尽可能地扩展,高度将根据比例进行调整。请参阅下面的示例以更好地理解。

<ImageView
android:id="@+id/my_img"
android:layout_width="0dp"
android:layout_height="0dp"
android:scaleType="centerCrop"
android:src="@drawable/img_test"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>

以上代码将生成一个方形图像,该图像将根据不同的父级宽度自行缩放。

编码愉快:)

关于android - 将 ImageView 大小与 ConstrainLayout 的比例用于响应性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49523872/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com