为什么 BottomAppBar 必须是 CoordinatorLayout 的 child?

Why does BottomAppBar must be child of CoordinatorLayout?

在所有样本中和 Material Design website BottomAppBar 用作 CoordinatorLayout 的直接 child。我读过这是推荐的做法。我已经将其作为 ConstraintLayout 的直接 child 进行了尝试,并且效果很好。为什么推荐这个?将其包装在 CoordinatorLayout 中的目的是什么?

ConstraintLayout 类似于 RelativeLayout,它提供了一种灵活的方式来创建平面层次结构视图。主要原则是帮助 relatively positioning 它的子视图。但是这种布局对处理父子视图之间的动画和交互没有太大帮助。

CoordinatorLayout 作为顶级布局引入,有助于处理和动画化其视图的转换,并为它们定义了一些 behavior motions

I have tried it as a direct child of ConstraintLayout and it works fine.

是的,您也可以将 BottomAppBar 放在 ConstrintLayout 中。但它将作为一个正常的子视图工作,没有任何运动行为。

Why is this recommended? What is the purpose of wrapping it up in CoordinatorLayout?

推荐,因为可以帮助您处理 BottomAppBar 的视图转换和运动行为。