自定义类中有 3 个构造方法;
public CustomView(Context context){}
public CustomView(Context context, @Nullable AttributeSet attrs){}
public CustomView(Context context, @Nullable AttributeSet attrs, int defStyleAttr){}
在 xml 文件中引用时, 走的是第二个构造方法. 所以应该在第二个构造中使用, 才能正常的获取到配置:
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomView);
Drawable background = typedArray.getDrawable(R.styleable.CustomView_background);