以编程方式向ConstraintLayout添加障碍的问题

时间:2019-06-28 13:50:49

标签: java android android-constraintlayout

我有两个基于XML的屏障,用于分隔要构建的日历中的某些按钮。这是XML:

<android.support.constraint.ConstraintLayout
    android:id="@+id/teamScheduleConstraintLayout"
    android:layout_width="match_parent"
    android:layout_height="500dp"
    app:layout_constraintTop_toTopOf="parent">

    <Button
        android:id="@+id/buttonPreviousMonth"
        android:layout_width="90dp"
        android:layout_height="50dp"
        android:text="@string/previousMonth"
        app:layout_constraintEnd_toStartOf="@+id/teamScheduleCurrentMonth"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/teamScheduleCurrentMonth"
        style="@style/textViewGeneral"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:gravity="center"
        android:text="July 2019"
        android:textSize="20sp"
        app:layout_constraintEnd_toStartOf="@+id/buttonNextMonth"
        app:layout_constraintStart_toEndOf="@+id/buttonPreviousMonth"
        app:layout_constraintTop_toTopOf="parent" />


    <Button
        android:id="@+id/buttonNextMonth"
        android:layout_width="90dp"
        android:layout_height="50dp"
        android:text="@string/nextMonth"
        android:textAlignment="center"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/teamScheduleCurrentMonth"
        app:layout_constraintTop_toTopOf="parent" />

    <android.support.constraint.Barrier
        android:id="@+id/monthSelectorBarrier"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        app:barrierDirection="bottom"
        app:constraint_referenced_ids="buttonPreviousMonth,teamScheduleCurrentMonth,buttonNextMonth" />


    <android.support.constraint.Barrier
        android:id="@+id/closeButtonBarrier"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        app:barrierDirection="top"
        app:constraint_referenced_ids="teamScheduleCloseButton"
        app:layout_constraintTop_toBottomOf="@id/monthSelectorBarrier" />

    <Button
        android:id="@+id/teamScheduleCloseButton"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:gravity="center"
        android:text="@string/CloseButton"
        android:textAlignment="center"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/closeButtonBarrier" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        tools:layout_editor_absoluteX="136dp"
        tools:layout_editor_absoluteY="211dp" />


</android.support.constraint.ConstraintLayout>

接下来,我要尝试以编程方式为日历中的每一天添加新的障碍:

Barrier newRow = new Barrier(getContext());
int barrierId = View.generateViewId();
newRow.setId(barrierId);
newRow.setType(Barrier.BOTTOM);
layout.addView(newRow);

假设我创建了两个新的障碍,即1和2。当我将某些TextView约束为“ @ + id / monthSelectorBarrier”时,它们将正确显示。但是,当我将它们限制为障碍1时,它们全都进入了视图的顶部:

Barrier

我使用类似于下面的代码将第一个新的障碍设置为直接位于“ @ + id / monthSelectorBarrier”下,但它仍位于ConstraintLayout的顶部。

以下是用于将程序创建的屏障设置为位于另一个程序创建的屏障下方的代码(allDays是TextViews ID的int数组,因此我可以将它们添加到每个屏障):

constraintSet.clone(layout);

constraintSet.connect(2, ConstraintSet.TOP, 1, ConstraintSet.BOTTOM);
constraintSet.connect(2, ConstraintSet.START, ConstraintSet.PARENT_ID, ConstraintSet.START);
constraintSet.connect(2, ConstraintSet.END, ConstraintSet.PARENT_ID, ConstraintSet.END);

constraintSet.connect(allDays[i], ConstraintSet.TOP, 2, ConstraintSet.BOTTOM);

constraintSet.applyTo(layout);

我需要改变什么才能使所有障碍正确地排在彼此下面?

1 个答案:

答案 0 :(得分:2)

这里有两个问题:

  1. 您忘记在以编程方式添加的屏障中设置 referenceIds
Barrier newRow = new Barrier(getContext());
int barrierId = View.generateViewId();
newRow.setId(barrierId);
newRow.setType(Barrier.BOTTOM);

// add the ids of the views for the barrier
int[] referenceIds = allDays; 
newRow.setReferencedIds(referenceIds);

layout.addView(newRow);
  1. 您需要为屏障生成的 viewId 设置约束:
...
constraintSet.connect(yourView.id, ConstraintSet.TOP, barrierId, ConstraintSet.BOTTOM);
...