android - View 之间的间距相等,其中一些在 ConstraintLayout 上使用父级的百分比

标签 android android-constraintlayout

我有一个 ConstraintLayout 并且我垂直对齐多个 View 。一些 View 使用 app:layout_constraintHeight_percent 使它们的高度成为整个布局/屏幕的百分比,而其他 View 使用 wrap_content 作为它们的高度。

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

这是一个示例布局:

<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>

这是现在的样子:

What I have right now

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

最佳答案

您应该将 View 放在 vertical chain 中展开额外的空间如下:

enter image description here

<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>

关于android - View 之间的间距相等,其中一些在 ConstraintLayout 上使用父级的百分比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53394769/

相关文章:

android - 自定义线性布局

java - 使用 openFileInput(Uri) 和多线程打开多个文件

android - wrap_content 在按钮高度android中不起作用

android - 如何将 View 与 TextView 中的文本垂直对齐?

android - 约束布局 : align top of textview with imageview

java - 模拟类将 null 分配给 @NonNull 字段(Android)

Javascript setTimeout 在 Android Samsung S4 上被阻止

android - ConstraintLayout 组与层

android - ConstraintLayout 准则下的边距未正确显示

java - 具有固定数量子项的 CardView