Android 中自定义属性declare-styleable的记录

在学习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);
            }
            ...
        }

        ...
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值