gpt4 book ai didi

android - 使用 ConstraintLayout 中具有纵横比的 View 填充剩余空间

转载 作者:太空宇宙 更新时间:2023-11-03 13:39:45 26 4
gpt4 key购买 nike

我在垂直链中有两个 View (viewA、viewB)。 viewA 的宽高比应始终为 1:1,而 viewB 的高度是动态的(可能是 400dp,可能是 700dp)

预期结果:当 viewB 的高度太大无法容纳屏幕中的两个 View 时,应减小 viewA 的宽度(保持宽高比)。

实际结果:viewA 的宽度始终与父级匹配,如果 viewB 的高度太大, View 会离开屏幕边界。

注意:最好使用 ConstantsLayout 并且不要嵌套 View 组。

<View
android:id="@+id/viewA"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/viewB"
app:layout_constraintDimensionRatio="h,1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_chainStyle="spread"/>

<View
android:id="@+id/viewB"
android:layout_width="match_parent"
android:layout_height="700dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/viewA"/>

最佳答案

将顶 View 的尺寸比例改为如下:

app:layout_constraintDimensionRatio="w,1:1"

现在,当底部 View 高度发生变化时,顶部将按照您的意愿进行跟踪,同时保持 1:1 的比例。我添加了颜色以更好地查看布局中的 View 。

700dp 的底 View

enter image description here

500dp 的底 View

enter image description here

200dp 的底 View

enter image description here

关于android - 使用 ConstraintLayout 中具有纵横比的 View 填充剩余空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55832832/

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