TL;DR
到目前为止,我的解决方案是:将 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 解决方案中,您不能在该包装器 ConstraintLayout 中设置填充,如 layout_constraintHeight_percent仅计算 inside 的大小(它忽略填充和边距)。但是在上面的共享包装解决方案中,您可以放心地设置填充(因为它们共享相同的父大小)。