自定義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();
}