【发布时间】:2019-05-12 15:19:37
【问题描述】:
我正在尝试绘制两个视图,其中一个将覆盖另一个,使用约束布局和约束指南,以及 app:layout_constraintDimensionRatio="h, 1:1" 来确定它们的大小(这是因为我将使用需要知道它们的自定义视图绘制时自己调整大小)
我正在尝试将一个布局的顶部和底部约束到另一个布局的底部,以确保它们相对于彼此定位,但似乎一旦我使用 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
如果我不得不猜测,我会说问题是因为我同时尝试将高度与导致高度为 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" />
【问题讨论】:
-
既然您使用固定位置作为您的指南,为什么不在您的 image_two 中使用“124dp”的高度和宽度并删除结束指南?这将把它放在你想要的地方
-
不幸的是,这是在没有意识到 XML 是如何变化的情况下移动指南的结果,它应该基于
app:layout_constraintGuide_percent。我已经更新了 xml 以反映这一点,很抱歉造成混淆
标签: android android-constraintlayout