如何根据另一个视图的高度/宽度按比例设置视图高度/宽度?

Posted

技术标签:

【中文标题】如何根据另一个视图的高度/宽度按比例设置视图高度/宽度?【英文标题】:How to set View height/width according to another View's height/width by ratio? 【发布时间】:2021-03-31 12:00:51 【问题描述】:

比如说,我有 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 仅与 parent 相关(而非约束)。

【问题讨论】:

【参考方案1】:

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_
    android:layout_>

    <TextView
        android:id="@+id/baseA"
        android:layout_
        android:layout_
        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_
        android:layout_
        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_
    android:layout_>

    <TextView
        android:id="@+id/baseA"
        android:layout_
        android:layout_
        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_
        android:layout_
        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_
            android:layout_
            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_
    android:layout_>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_
        android:layout_
        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_
            android:layout_
            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_
            android:layout_
            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 的大小(它忽略填充和边距)。但是在上面的共享包装解决方案中,您可以放心地设置填充(因为它们共享相同的父大小)。

【讨论】:

以上是关于如何根据另一个视图的高度/宽度按比例设置视图高度/宽度?的主要内容,如果未能解决你的问题,请参考以下文章

CSS3如何固定图片宽度使图片高度按图片比例自适应?

MainWindow 高度\宽度将根据当前视图 WPF 设置

CSS3如何固定图片宽度使图片高度按图片比例自适应?

如何让一个元素高度随宽度按比例自适应

iOS 约束。如何通过约束设置图像视图高度比率等于图像比率?

如何使 UIViews 按比例增长