//获取自定义属性
private void initAttrs(Context context, AttributeSet attrs) {
//从属性文件里获取属性集合
TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.MyCustomButton );
//获取属性
int num = typedArray.getInt(R.styleable.MyCustomButton_bttextNum, 0);
int resourceId = typedArray.getResourceId(R.styleable.MyCustomButton_btbackground, 1);
//把属性设置给自定义的控件
setText(num + "");
setBackgroundResource(resourceId);
//利用
typedArray.recycle();
private void initAttrs(Context context, AttributeSet attrs) {
//从属性文件里获取属性集合
TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.MyCustomButton );
//获取属性
int num = typedArray.getInt(R.styleable.MyCustomButton_bttextNum, 0);
int resourceId = typedArray.getResourceId(R.styleable.MyCustomButton_btbackground, 1);
//把属性设置给自定义的控件
setText(num + "");
setBackgroundResource(resourceId);
//利用
typedArray.recycle();
}
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
</resources>
<resources>
<declare-styleable name="MyCustomButton">
<attr name="bttextNum" format="integer"></attr>
<attr name="btbackground" format="reference|color"></attr>
</declare-styleable>
</resources>