【问题标题】:Equal spacing between views some of which use percentage of parent on a ConstraintLayout视图之间的相等间距,其中一些使用 ConstraintLayout 上的父级百分比
【发布时间】:2018-11-20 22:31:37
【问题描述】:

我有一个ConstraintLayout,我正在垂直对齐几个视图。一些视图使用app:layout_constraintHeight_percent 使其高度占整个布局/屏幕的百分比,而其他视图使用wrap_content 作为它们的高度。

我想要的是将剩余的垂直空间拆分为每个视图之间的相等间距。这可能吗?有什么策略可以实现吗?

这是一个示例布局:

<android.support.constraint.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"
    android:background="#FFFFFF">

    <FrameLayout
        android:id="@+id/frame1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:background="#FF0000"
        app:layout_constraintHeight_percent="0.1"
        app:layout_constraintTop_toTopOf="parent" />

    <FrameLayout
        android:id="@+id/frame2"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:background="#FFFF00"
        app:layout_constraintHeight_percent="0.2"
        app:layout_constraintTop_toBottomOf="@+id/frame1" />

    <FrameLayout
        android:id="@+id/frame3"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:background="#FF00FF"
        app:layout_constraintHeight_percent="0.3"
        app:layout_constraintTop_toBottomOf="@+id/frame2" />

    <FrameLayout
        android:id="@+id/frame4"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:background="#00FFFF"
        app:layout_constraintTop_toBottomOf="@+id/frame3" />
</android.support.constraint.ConstraintLayout>

这是它现在的样子:

我想要实现的是在每个彩色矩形之间有相等的空白。

【问题讨论】:

    标签: android android-constraintlayout


    【解决方案1】:

    您应该将视图放置在 vertical chain 内,以按如下方式展开额外空间:

    <android.support.constraint.ConstraintLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#FFFFFF">
    
        <FrameLayout
            android:id="@+id/frame1"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:background="#FF0000"
            app:layout_constraintBottom_toTopOf="@+id/frame2"
            app:layout_constraintHeight_percent="0.1"
            app:layout_constraintVertical_chainStyle="spread_inside"
            app:layout_constraintTop_toTopOf="parent" />
    
        <FrameLayout
            android:id="@+id/frame2"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:background="#FFFF00"
            app:layout_constraintBottom_toTopOf="@+id/frame3"
            app:layout_constraintHeight_percent="0.2"
            app:layout_constraintTop_toBottomOf="@+id/frame1" />
    
        <FrameLayout
            android:id="@+id/frame3"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:background="#FF00FF"
            app:layout_constraintBottom_toTopOf="@+id/frame4"
            app:layout_constraintHeight_percent="0.3"
            app:layout_constraintTop_toBottomOf="@+id/frame2" />
    
        <FrameLayout
            android:id="@+id/frame4"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:background="#00FFFF"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/frame3" />
    </android.support.constraint.ConstraintLayout>
    

    【讨论】:

    • 谢谢。我已经得出了相同的结论,但很高兴得到确认。这有助于我为stackoverflow准备问题。我已经尝试过使用带百分比的链,但我将大于 1 的值用于百分比,将其与权重混淆。