1、在res/values文件下定义一个attrs.xml文件
2、新建一个MyView.java类,继承自View,实例化onPaint(),再写构造函数如下:
public MyView(Context context,AttributeSet attrs)
{
super(context,attrs);
mPaint = new Paint();
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.MyView);
int textColor = a.getColor(R.styleable.MyView_textColor,
0XFFFFFFFF);
float textSize = a.getDimension(R.styleable.MyView_textSize, 36);
mPaint.setTextSize(textSize);
mPaint.setColor(textColor);
a.recycle();
}
注意默认值,防止没有定义
注意recycle(),回收使用
3、在layout中注意加上命名空间, xmlns:test ="http://schemas.android.com/apk/res/com.android
其中com.andriod是包名,用法和android前缀是相同的