一、 在res/values 文件下定义一个attrs.xml 文件
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="MyButton">
<attr name="textColor" format="color" />
<attr name="textSize" format="dimension" />
</declare-styleable>
</resources>
二、在<declare-styleable name="MyButton"></declare-styleable> 里的 名字,获取里面属性
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyButton);
int n = a.getIndexCount();
for (int i = 0; i < n; i++) {
int attr = a.getIndex(i);
switch (attr) {
case R.styleable.MyButton_textColor:
mColor =a.getColor(R.styleable.MyButton_textColor, 0XFFFFFFFF);
setColor(mColor);
break;
case R.styleable.MyButton_TextSize:
mTextSize = a.getDimension(attr, 36);
setTextSize(mTextSize);
break;
}
}
a.recycle();