如何根据另一个View的height/width按比例设置Viewheight/width?

How to set View height/width according to another View's height/width by ratio?

说,我有 2 个 View:A 和 B。A 的大小设置为 wrap_content,导致它的大小可能会随着内容的变化而变化:文本大小、图像大小、数据量.. .等等

我想要视图 B 的 height/width 与视图 A 相关,但按比例----例如,B 的高度是 A 的 的 1/3。我怎样才能实现它?

据我所知,ConstraintLayout应该会来救援。但我只能做到 通过设置 constraintTop_toTopOf="@+id/viewA"app:layout_constraintBottom_toBottomOf="@+id/viewA" 等于。我尝试使用 Guideline 来定位“专用高度”——但似乎 Guideline 仅与 parent 有关(不是约束)。

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相同的问题:它们是根据parent's size计算的,无论它们有什么约束:

有人说 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中,并将其height/width设置为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>

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