首页 > 解决方案 > 空初始约束集和运动布局上的“deriveConstraintsFrom”问题

问题描述

我目前正在迈出运动布局的第一步。

我要创建的示例如下:

想要的结果

一个包含两个 FAB 的运动布局(一个迷你和一个普通的)。上图显示了单击正常大小的 FAB 后的结束状态。


我试过的:

运动布局(是其他布局文件的一部分):

<androidx.constraintlayout.motion.widget.MotionLayout
            android:id="@+id/include"
            app:motionDebug="SHOW_PATH"
            android:layout_width="0dp"
            android:layout_height="0dp"

            android:background="@android:color/darker_gray"
            android:layout_margin="8dp"
            app:layoutDescription="@xml/menu_scene"
            app:layout_constraintTop_toBottomOf="@id/text"
            app:layout_constraintStart_toStartOf="@id/text"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintBottom_toBottomOf="parent">

            <com.google.android.material.floatingactionbutton.FloatingActionButton
                android:id="@+id/images_button"

                android:layout_width="wrap_content"
                android:layout_height="wrap_content"

                app:layout_constraintStart_toStartOf="@id/menu_button"
                app:layout_constraintTop_toTopOf="@id/menu_button"
                app:layout_constraintBottom_toBottomOf="@id/menu_button"

                android:src="@drawable/ic_image"
                app:fabSize="mini" />

            <com.google.android.material.floatingactionbutton.FloatingActionButton
                android:id="@+id/menu_button"

                android:layout_width="wrap_content"
                android:layout_height="wrap_content"

                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"

                android:src="@drawable/ic_edit" />

        </androidx.constraintlayout.motion.widget.MotionLayout>

运动场景:

<MotionScene xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <Transition
        app:constraintSetStart="@+id/start"
        app:constraintSetEnd="@+id/end">

        <OnClick
            app:targetId="@+id/menu_button"
            app:clickAction="toggle" />
    </Transition>
<!-- pulls constraints from layout -->
    <ConstraintSet android:id="@+id/start" />

    <ConstraintSet android:id="@+id/end">
        <Constraint android:id="@id/images_button">
            <Layout
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintBottom_toBottomOf="parent" />
        </Constraint>
    </ConstraintSet>
</MotionScene>

但不幸的是,通过这种配置,我得到了这个:

错误的结果

左下角的小粉红点是迷你 FAB。


因为我听说了derivedConstraintsFrom我给了它一个机会:

<MotionScene ...>

    <Transition ...>
       ...
    </Transition>

    <ConstraintSet android:id="@+id/start" />

    <ConstraintSet
        android:id="@+id/end"
        app:deriveConstraintsFrom="@id/start">

        <Constraint android:id="@id/images_button">
           ...
        </Constraint>

    </ConstraintSet>
</MotionScene>

但我得到了同样的结果!:(


我得到想要的结果的唯一方法是使用以下约束集配置:

<MotionScene ...>

    ...

    <ConstraintSet android:id="@+id/start" />

    <ConstraintSet android:id="@+id/end">
        <Constraint android:id="@id/images_button">

            <!--  duplicated the layout properties from the initial layout :( -->
            <Layout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:layout_constraintTop_toTopOf="@id/menu_button"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintBottom_toBottomOf="parent" />
        </Constraint>
    </ConstraintSet>
</MotionScene>

包起来:

我认为空约束集<ConstraintSet android:id="@+id/start" />从布局本身中提取其约束,并且app:deriveConstraintsFrom="@id/start"在另一个约束集上指定来自初始约束集的所有约束并覆盖它自己的约束。

有人可以告诉我为什么这不起作用吗?还是我错过了什么?

谢谢,克里斯

PS.:我使用androidx.constraintlayout:constraintlayout:2.0.0-beta4

标签: androidandroid-motionlayout

解决方案


推荐阅读