问题描述
我最近尝试过MotionLayout
,当按钮是MotionLayout
的直接子代时,我可以在按钮上正常工作,但是当我将按钮封装在另一个布局中时,相同的运动场景不起作用,但父级布局仍为MotionLayout
。
按钮为直接子级的第一种布局:-
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.motion.widget.MotionLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
app:layoutDescription="@xml/demo"
android:layout_height="match_parent"
tools:context=".Demo" >
<Button
android:layout_width="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_height="wrap_content"
android:id="@+id/yellow_button"
/>
</androidx.constraintlayout.motion.widget.MotionLayout>
第二个布局,其中按钮是间接子元素:-
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.motion.widget.MotionLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
app:layoutDescription="@xml/demo"
android:layout_height="match_parent"
tools:context=".Demo"
>
<LinearLayout
android:layout_width="match_parent"
android:id="@+id/l1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/yellow_button"/>
</LinearLayout>
</androidx.constraintlayout.motion.widget.MotionLayout>
运动场景的布局如下:-
<?xml version="1.0" encoding="utf-8"?>
<MotionScene xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<ConstraintSet android:id="@+id/start">
<Constraint android:id="@+id/yellow_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" >
<CustomAttribute app:attributeName="alpha"
app:customFloatValue="0.0"/>
</Constraint>
</ConstraintSet>
<ConstraintSet android:id="@+id/end">
<Constraint android:id="@id/yellow_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:alpha="1.0"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
>
<CustomAttribute app:attributeName="alpha"
app:customFloatValue="1.0"/>
</Constraint>
</ConstraintSet>
<Transition
app:constraintSetEnd="@id/end"
app:autoTransition="animateToEnd"
app:constraintSetStart="@+id/start"
app:duration="2000"/>
在这些情况下是否需要遵循任何准则?
OR
这是否意味着只有MotionLayout
的直接子代可以使用动画?
解决方法
Google Developers的这篇中等文章(https://medium.com/google-developers/introduction-to-motionlayout-part-i-29208674b10d)在“限制”部分下说: “ MotionLayout将仅为其直接子级提供功能,这与TransitionManager相反,后者可以与嵌套的布局层次结构以及Activity过渡一起使用。”