gpt4 book ai didi

android - 是否可以使用 constraintDimensionRatio 和高度/宽度 match_parent 绘制布局,同时将布局的顶部和底部限制为一点

转载 作者:行者123 更新时间:2023-11-30 05:11:11 26 4
gpt4 key购买 nike

我正在尝试绘制两个 View ,其中一个将覆盖另一个,使用约束布局和 constraint_guidelines,以及 app:layout_constraintDimensionRatio="h, 1:1" 调整它们的大小(这是因为我将使用自定义 View ,它们在绘制时需要知道自己的大小)

我试图将一个布局的顶部和底部约束到另一个布局的底部,以确保它们相对于彼此定位,但似乎只要我使用 match_parent 来调整它们的大小,约束就不允许它们相互重叠。

This is how I'd like them to lay out;

请注意,上面的屏幕截图是在手动放置较小的 imageView 的情况下拍摄的

This is how they layout with both the second images dimensions set to match_parent, and ratio set

This is the same, but with height/width of the smaller image view set to 120dp rather than match_parent

如果非要我猜的话,我会说这个问题是因为我同时尝试将高度与会导致高度为 0dp 的约束相匹配,同时还给它一个比例以确保其高度与宽度相匹配指南之间。

下面是 XML。如果有人能告诉我如何在从指南之间的宽度导出高度的同时将布局限制在一个点上,将不胜感激

<androidx.constraintlayout.widget.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<androidx.constraintlayout.widget.Guideline
android:id="@+id/example_guideline_left_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.3"
app:layout_constraintStart_toStartOf="parent" />

<androidx.constraintlayout.widget.Guideline
android:id="@+id/onboarding_role_guideline_right_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.7"
app:layout_constraintStart_toStartOf="parent" />

<ImageView
android:id="@+id/example_image_one"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@color/colorPrimaryDark"
app:layout_constraintDimensionRatio="h, 1:1"
app:layout_constraintEnd_toStartOf="@+id/onboarding_role_guideline_right_one"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="@+id/example_guideline_left_one"
app:layout_constraintTop_toTopOf="parent" />

<androidx.constraintlayout.widget.Guideline
android:id="@+id/example_guideline_left_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.55"
app:layout_constraintStart_toStartOf="parent" />

<androidx.constraintlayout.widget.Guideline
android:id="@+id/onboarding_role_guideline_right_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.85"
app:layout_constraintStart_toStartOf="parent" />

<ImageView
android:id="@+id/example_image_two"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintDimensionRatio="h, 1:1"
app:layout_constraintBottom_toBottomOf="@+id/example_image_one"
app:layout_constraintEnd_toStartOf="@+id/onboarding_role_guideline_right_two"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="@+id/example_guideline_left_two"
app:layout_constraintTop_toBottomOf="@+id/example_image_one" />

最佳答案

实现了一个非常简单的解决方案,认为这是一个过于拘泥于特定约束而无法只见树木不见森林的问题。

创建一个 <Space>元素,将其设置为约束在较大图像的顶部和底部,然后将较小图像的顶部约束到那个。然后可以调整layout_constraintVertical_bias<Space>按要求。

关于android - 是否可以使用 constraintDimensionRatio 和高度/宽度 match_parent 绘制布局,同时将布局的顶部和底部限制为一点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53727445/

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