Android MotionLayout: MotionScene not triggered



我使用 MotionLayout 构建了一个非常简单的示例,但由于某种原因,onClick 没有触发动画

我错过了什么??

我有以下布局:

<androidx.constraintlayout.motion.widget.MotionLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
app:layoutDescription="@xml/scene_substitution"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
>
<View
android:id="@+id/animateMe"
android:layout_width="50dp"
android:layout_height="500dp"
android:background="@android:color/holo_red_dark"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
/>
<Button
android:id="@+id/startAnimation"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:text="Click me"
/>
</androidx.constraintlayout.motion.widget.MotionLayout>

而斩杀场景描述:

<?xml version="1.0" encoding="utf-8"?>
<MotionScene xmlns:motion="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<Transition
motion:constraintSetStart="@+id/start"
motion:constraintSetEnd="@+id/end"
motion:duration="1000">
<OnClick
app:targetId="@+id/startAnimation"
app:clickAction="toggle"
/>
</Transition>
<ConstraintSet android:id="@+id/start">
<Constraint
android:id="@+id/animateMe"
android:layout_width="100dp"
android:layout_height="400dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:background="@android:color/holo_blue_bright"
/>
</ConstraintSet>
<ConstraintSet android:id="@+id/end">
<Constraint
android:id="@+id/animateMe"
android:layout_width="300dp"
android:layout_height="100dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:background="@android:color/holo_green_dark"
/>
</ConstraintSet>
</MotionScene>

终于明白了! 我为xmlns:motion使用了错误的命名空间值。

当然,它应该是xmlns:motion="http://schemas.android.com/apk/res-auto"...

最新更新