TL;博士
到目前为止,我的解决方案是:与包装器结合layout_constraintHeight_percent
(或layout_constraintWidth_percent
) 。ConstraintLayout
“百分比”的限制
我四处问人。有人说在这种情况下应该使用layout_constraintHeight_percent
(或):layout_constraintWidth_percent
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/baseA"
android:layout_width="100dp"
android:layout_height="300dp"
android:background="@color/teal_200"
android:gravity="center"
android:text="base A"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:layout_width="100dp"
android:layout_height="0dp"
android:background="@color/purple_200"
android:gravity="center"
android:text="base B"
app:layout_constraintBottom_toBottomOf="@+id/baseA"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.7"
app:layout_constraintHeight_percent="0.33"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/baseA" />
</androidx.constraintlayout.widget.ConstraintLayout>
但是,layout_constraintHeight_percent
遇到了与以下相同的问题Guideline
:它们是根据父级的大小计算的,无论它们有什么约束:

有人说layout_constrainedHeight
应该设置为true
,但没有任何改变:

适配:B-specified Wrapper
上面的行为给了我一个想法:既然constraint percent
只由父级计算,那么我们可以修改父级——通过给它一个自定义的。
更改上面的代码,将视图 B 带到一个新的ConstraintLayout
,将 B 迁移到其中,约束到父级,然后percent
再次使用:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/baseA"
android:layout_width="100dp"
android:layout_height="300dp"
android:background="@color/teal_200"
android:gravity="center"
android:text="base A"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="wrap_content"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="@+id/baseA"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.7"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/baseA">
<TextView
android:layout_width="100dp"
android:layout_height="0dp"
android:background="@color/purple_200"
android:gravity="center"
android:text="base B"
app:layout_constrainedHeight="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_percent="0.33"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

现在它就像一个魅力。
更好的方法:在同一个包装内
另一种解决方案是将 A 和 B 放在同一个ConstraintLayout
中,并将其高度/宽度设置为wrap_content
,使其大小等于 A:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="@+id/baseA"
android:layout_width="100dp"
android:layout_height="300dp"
android:background="@color/teal_200"
android:gravity="center"
android:text="base A"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:layout_width="100dp"
android:layout_height="0dp"
android:background="@color/purple_200"
android:gravity="center"
android:text="base B"
app:layout_constrainedHeight="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.7"
app:layout_constraintHeight_percent="0.33"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

而且我认为这是一个更好的解决方案——因为在 only-B-wrapper 解决方案中,您不能在该 wrapper 中设置填充ConstraintLayout
,因为只能在内部layout_constraintHeight_percent
计算大小(它忽略填充和边距)。但是在上面的共享包装器解决方案中,您可以毫无顾虑地设置填充(因为它们共享相同的父大小)。