Android Support Library 24.2.0 小坑

近日,Android Support Library升级到24.2,最大特点就是将库分割成若干小模块,以供使用(其实是因为Support Library已经方法数超多了……)

今天重新打开一个练手项目,发现运行时……居然崩了!

崩溃地点在

android.support.design.widget.FloatingActionButton$Behavior.isBottomSheet

内容为:

java.long.ClassCastException: android.support.design.widget.AppBarLayout$LayoutParams cannot be cast to android.support.design.widget.CoordinatorLayout$LayoutParams

找了一圈,终于发现在这里:FAB can no longer be anchored to indirect children of CoordinatorLayout

FAB can no longer be anchored to indirect children of CoordinatorLayout

Library used : design
Version used: 24.2.0
Theme used: Theme.AppCompat.Light.DarkActionBar
Devices/Android versions reproduced on: Android 6.0 Emulator

Prior to support library version 24.2.0, a FAB could be anchored to a nested child of a CoordinatorLayout.

For example, with the following view hierarchy:

<CoordinatorLayout>
<LinearLayout>
<LinearLayout>
<View>
</LinearLayout>
</LinearLayout>
<Fab>
</CoordinatorLayout>

The FAB could be anchored to View.

With 24.2.0, the app now crashes with the exception:

android.widget.LinearLayout$LayoutParams cannot be cast to android.support.design.widget.CoordinatorLayout$LayoutParams

This is due to the isBottomSheet() method in the FloatingActionButton class. > It assumes that the view has CoordinatorLayout LayoutParams.

其实,说白了就一句话,FloatingActionButton的锚点(Anchor)不能再是CoordinatorLayout的非直接子项了(indirect children)。

我之前的layout种没有指定anchor而是直接使用android_layoutGravity将FAB固定在右下方,将FAB的锚点指定为CoordinatorLayout的直接子项就可以了。