自定义view属性
1.在values下新建 attr.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="MyTextView">
<attr name="myIcon" format="reference"/>
</declare-styleable>
</resources>
2.布局中使用该view
3. 拿到自定义属性的值
TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.MyTextView, 0, 0);
try {
int id =typedArray.getResourceId(R.styleable.MyTextView_myIcon,0);
if (id!=0){
Drawable clearDraw = ContextCompat.getDrawable(getContext(),id);
}
} finally {
typedArray.recycle();
}