在学习CoordinatorLayout布局中有一个属性是behave,这个属性很神奇,他的作用对象不是CoordinatorLayout本身而是其子View,仔细看源码这个属性定义的declare-styleable既不是在CoordinatorLayout中也不是NestScrollView,而是一个单独的定义
这个属性定义的是CoordinatorLayout_Layout,后来发现这里Android会将带XXX_Layout的属性范围放在XXXView的子View中,将子View和父View解耦,同时满足父子View间的联系。例如这里的behave。
对于这个属性的解析也是在CoordinatorLayout中,不过是单独抽出来一个静态内部类LayoutParams解析控件的参数,
public static class LayoutParams extends MarginLayoutParams {
...
Behavior mBehavior;
boolean mBehaviorResolved = false;
LayoutParams(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
final TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.CoordinatorLayout_Layout);
...
dodgeInsetEdges = a.getInt(
R.styleable.CoordinatorLayout_Layout_layout_dodgeInsetEdges, 0);
mBehaviorResolved = a.hasValue(
R.styleable.CoordinatorLayout_Layout_layout_behavior);
if (mBehaviorResolved) {
mBehavior = parseBehavior(context, attrs, a.getString(
R.styleable.CoordinatorLayout_Layout_layout_behavior));
}
a.recycle();
if (mBehavior != null) {
// If we have a Behavior, dispatch that it has been attached
mBehavior.onAttachedToLayoutParams(this);
}
...
}
...
}