0

说,我有 2 View:A 和 B。A 的大小设置为wrap_content,导致其大小可能会在内容变化时发生变化:文本大小、图像大小、数据量......等。

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

据我所知,ConstraintLayout应该是来救援的。但我只能通过设置constraintTop_toTopOf="@+id/viewA"和来使其相等app:layout_constraintBottom_toBottomOf="@+id/viewA"。我尝试使用Guideline来定位“专用高度”——但它似乎Guideline只与父级有关(而不是约束)。

4

1 回答 1

0

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,但没有任何改变:

约束高度百分比 + 约束高度 = 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计算大小(它忽略填充和边距)。但是在上面的共享包装器解决方案中,您可以毫无顾虑地设置填充(因为它们共享相同的父大小)。

于 2020-12-21T17:47:02.913 回答