MotionLayout 是 ConstraintLayout 2.0 版本引入进来的,目前还在测试版本中,但感觉还挺有意思的,就写一篇记录一下。
MotionLayout 类继承自 ConstraintLayout 类,允许你为各种状态之间的布局设置过渡动画。由于 MotionLayout 继承了 ConstraintLayout,因此可以直接在 XML 布局文件中使用 MotionLayout 替换 ConstraintLayout。MotionLayout 是完全声明式的,你可以完全在 XML 文件中描述一个复杂的过渡动画而无需任何代码。
MotionLayout 与 ConstraintLayout 不同的是 MotionLayout 需要链接到一个 MotionScene 文件。使用 MotionLayout 的 app:layoutDescription 属性将 MotionLayout 链接到一个 MotionScene 文件。另外,MotionLayout 所有的直接子 View 需要指定 id,不然会报错:
1 | All children of ConstraintLayout must have ids to use ConstraintSet. |