在 ConstraintLayout 内向准则 objects 添加边距

Add margin to guideline objects inside ConstraintLayout

这可能吗?

我想要关注:

我能用 ConstraintLayout 以某种方式实现吗?

问题

我想添加一个固定高度的 header 并且我希望百分比约束不根据 ConstraintLayout 的高度而是根据剩余的 space 来计算它们的百分比在减去 header.

后的 ConstraintLayout

重要提示

是的,我可以将整个 ConstraintLayout 包裹在 LinearLayout 中,但我对仅基于 ConstraintLayout 的解决方案感兴趣。

编辑 1

为了更清楚,我想要以下内容:

h_full... full height of ConstraintLayout
h_header... full height of header

Vertical arrangement of views should look like following:
    - HEADER - y = 0
    - VIEW 1 - y = h_header
    - VIEW 2 - y = h_header + (h_full - h_header) / 4
    - VIEW 3 - y = h_header + (h_full - h_header) / 4 * 2
    - VIEW 4 - y = h_header + (h_full - h_header) / 4 * 3

Let's assume following:
   h_full = 1000
   h_header = 100
   space for views = 1000 - 100 = 900 (this is the base for the percentages!)
  h_view = 900 / 4 = 225

Then we get following:
    - HEADER - y = 0
    - VIEW 1 - y = 100
    - VIEW 2 - y = 100 + 225
    - VIEW 3 - y = 100 + 225 * 2
    - VIEW 4 - y = 100 + 225 * 3

代码

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

    <!-- Guidelines -->
    <!-- 3 horizontal guidelines -->

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline_horizontal1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.25" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline_horizontal2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.5" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline_horizontal3"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.75" />

    <!-- Views - HEADER -->

    <View
        android:id="@+id/header"
        android:layout_width="0dp"
        android:layout_height="64dp"
        android:layout_marginLeft="@dimen/calender_cell_padding"
        android:layout_marginTop="@dimen/calender_cell_padding"
        android:layout_marginRight="@dimen/calender_cell_padding"
        android:layout_marginBottom="@dimen/calender_cell_padding"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

     <!-- Views - 4 Views, evenly distrubuted vertically -->
     
    <View
        android:id="@+id/view1"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@id/guideline_horizontal1"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <View
        android:id="@+id/view2"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@id/guideline_horizontal2"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="parent"
        app:layout_constraintTop_toBottomOf="@id/guideline_horizontal1" />

    <View
        android:id="@+id/view3"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@id/guideline_horizontal3"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="parent"
        app:layout_constraintTop_toBottomOf="@id/guideline_horizontal2" />

    <View
        android:id="@+id/view4"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="parent"
        app:layout_constraintTop_toBottomOf="@id/guideline_horizontal3" />

</androidx.constraintlayout.widget.ConstraintLayout>

HEADER的解决方案:

app:layout_constraintHorizontal_bias and/or app:layout_constraintVertical_bia 可用的 space 允许时,使用偏差修改视图位置。 例如:

<Button
    ...
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintHorizontal_bias="0.25"
    .../>

或者,您可以像这样使用带有 app:layout_constraintGuide_percent 的指南: 例如:

<androidx.constraintlayout.widget.Guideline
    android:id="@+id/guideline"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    app:layout_constraintGuide_percent="0.5"/>

4 行均匀分布高度的解决方案(视图高度的 25% - header 高度)

您可以使用 ConstraintLayout.widget.Flow 获得相同的 .可以在此处找到相同的文档:https://developer.android.com/reference/androidx/constraintlayout/helper/widget/Flow

它可以帮助您以一种受限的方式限制只有一种宽度和高度的所有所需视图:


    <androidx.constraintlayout.helper.widget.Flow
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" 
        app:constraint_referenced_ids="item_1,item_2,item_3" />

reference_ids 是您以受限方式放置所有需要的视图的地方。还可以选择使用不同的环绕模式来排列视图。您可以从文档中探索环绕模式。

我找到了解决方案,方法是使用 LinearLayouts 构建我的布局,然后将其转换为 ConstraintLayout,然后我做了一些调整 - 它奏效了。

解决方案是使用没有指南的双向绑定 - 这样你就可以定义一个固定高度的 header 并双向绑定下面的所有视图0dp 的高度,这将导致 header 以下的所有视图彼此平等地共享 remaining space。

这是我的完整布局,它是带有 header 的周视图(header 具有固定大小,日单元格确实使用剩余的 space):

截图

布局

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

    <!-- Header -->

    <com.google.android.material.card.MaterialCardView
        android:id="@+id/mcvDetails"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/calender_cell_padding"
        android:layout_marginTop="@dimen/calender_cell_padding"
        android:layout_marginEnd="@dimen/calender_cell_padding"
        android:layout_marginBottom="@dimen/calender_cell_half_padding"
        app:layout_constraintBottom_toTopOf="@+id/btFriday"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <LinearLayout
            android:id="@+id/llDayDetails"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="4dp"
            android:orientation="vertical">

            <TextView
                android:id="@+id/tvInfos"
                android:gravity="center_horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Infos" />

        </LinearLayout>

    </com.google.android.material.card.MaterialCardView>

    <!-- Row 1 - Mon / Fri -->

    <com.michaelflisar.iron.feature.workoutlog.views.calendarview.cell.CalendarCellView
        android:id="@+id/btMonday"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="@dimen/calender_cell_padding"
        android:layout_marginEnd="@dimen/calender_cell_half_padding"
        app:layout_constraintBottom_toBottomOf="@+id/btFriday"
        app:layout_constraintEnd_toStartOf="@+id/btFriday"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/btFriday"
        app:vcc_label="@string/monday" />

    <com.michaelflisar.iron.feature.workoutlog.views.calendarview.cell.CalendarCellView
        android:id="@+id/btFriday"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="@dimen/calender_cell_half_padding"
        android:layout_marginTop="@dimen/calender_cell_half_padding"
        android:layout_marginEnd="@dimen/calender_cell_padding"
        android:layout_marginBottom="@dimen/calender_cell_half_padding"
        app:layout_constraintBottom_toTopOf="@+id/btSaturday"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/btMonday"
        app:layout_constraintTop_toBottomOf="@+id/mcvDetails"
        app:vcc_label="@string/friday" />

    <!-- Row 2 - Tue / Sat -->

    <com.michaelflisar.iron.feature.workoutlog.views.calendarview.cell.CalendarCellView
        android:id="@+id/btTuesday"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="@dimen/calender_cell_padding"
        android:layout_marginEnd="@dimen/calender_cell_half_padding"
        app:layout_constraintBottom_toBottomOf="@+id/btSaturday"
        app:layout_constraintEnd_toStartOf="@+id/btSaturday"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/btSaturday"
        app:vcc_label="@string/tuesday" />

    <com.michaelflisar.iron.feature.workoutlog.views.calendarview.cell.CalendarCellView
        android:id="@+id/btSaturday"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="@dimen/calender_cell_half_padding"
        android:layout_marginTop="@dimen/calender_cell_half_padding"
        android:layout_marginEnd="@dimen/calender_cell_padding"
        android:layout_marginBottom="@dimen/calender_cell_half_padding"
        app:layout_constraintBottom_toTopOf="@+id/btSunday"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/btTuesday"
        app:layout_constraintTop_toBottomOf="@+id/btFriday"
        app:vcc_label="@string/saturday" />

    <!-- Row 3 - Wen / Sun -->

    <com.michaelflisar.iron.feature.workoutlog.views.calendarview.cell.CalendarCellView
        android:id="@+id/btWednesday"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="@dimen/calender_cell_padding"
        android:layout_marginEnd="@dimen/calender_cell_half_padding"
        app:layout_constraintBottom_toBottomOf="@+id/btSunday"
        app:layout_constraintEnd_toStartOf="@+id/btSunday"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/btSunday"
        app:vcc_label="@string/wednesday" />

    <com.michaelflisar.iron.feature.workoutlog.views.calendarview.cell.CalendarCellView
        android:id="@+id/btSunday"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="@dimen/calender_cell_half_padding"
        android:layout_marginTop="@dimen/calender_cell_half_padding"
        android:layout_marginEnd="@dimen/calender_cell_padding"
        android:layout_marginBottom="@dimen/calender_cell_half_padding"
        app:layout_constraintBottom_toTopOf="@+id/btUnused"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/btWednesday"
        app:layout_constraintTop_toBottomOf="@+id/btSaturday"
        app:vcc_label="@string/sunday" />

    <!-- Row 4 - Thu -->

    <com.michaelflisar.iron.feature.workoutlog.views.calendarview.cell.CalendarCellView
        android:id="@+id/btThursday"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="@dimen/calender_cell_padding"
        android:layout_marginEnd="@dimen/calender_cell_half_padding"
        app:layout_constraintBottom_toBottomOf="@+id/btUnused"
        app:layout_constraintEnd_toStartOf="@+id/btUnused"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/btUnused"
        app:vcc_label="@string/thursday" />

    <View
        android:id="@+id/btUnused"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="@dimen/calender_cell_half_padding"
        android:layout_marginTop="@dimen/calender_cell_half_padding"
        android:layout_marginEnd="@dimen/calender_cell_padding"
        android:layout_marginBottom="@dimen/calender_cell_padding"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/btThursday"
        app:layout_constraintTop_toBottomOf="@+id/btSunday" />

</androidx.constraintlayout.widget.ConstraintLayout>