我有一个 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>
这是现在的样子:
我想要实现的是在每个彩色矩形之间有相等的空白。
最佳答案
您应该将 View 放在 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>
关于android - View 之间的间距相等,其中一些在 ConstraintLayout 上使用父级的百分比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53394769/