自定义属性
布局文件
<
com
.example.administrator.myapplication.numberAddSubView
android :id= "@+id/numberAddView"
whq :number_background= "@drawable/add_sub_number_view_bg"
whq :minValue= "1"
whq :maxValue= "10"
whq :value= "2"
android :layout_width= "wrap_content"
android :layout_height= "wrap_content" />
android :id= "@+id/numberAddView"
whq :number_background= "@drawable/add_sub_number_view_bg"
whq :minValue= "1"
whq :maxValue= "10"
whq :value= "2"
android :layout_width= "wrap_content"
android :layout_height= "wrap_content" />
values包下创建attrs.xml文件
<?
xml version=
"1.0"
encoding=
"utf-8"
?>
< resources>
< declare-styleable name= "numberAddSubView">//view的名称
< attr name= "value" format= "integer|reference"/>
< attr name= "minValue" format= "integer|reference"/>
< attr name= "maxValue" format= "integer|reference"/>
< attr name= "number_background" format= "reference"/>//引用类型
</ declare-styleable>
< resources>
< declare-styleable name= "numberAddSubView">//view的名称
< attr name= "value" format= "integer|reference"/>
< attr name= "minValue" format= "integer|reference"/>
< attr name= "maxValue" format= "integer|reference"/>
< attr name= "number_background" format= "reference"/>//引用类型
</ declare-styleable>
</resources>
可以在activity代码中设置value 等的值//前提是有set方法
numberAddView = (numberAddSubView)findViewById(R.id.
numberAddView);
numberAddView.setValue(15);
自定义属性并获取属性值
咱们自定义的属性都在attrs中
public numberAddSubView(Context context, AttributeSet attrs,
int defStyleAttr) {
super(context, attrs, defStyleAttr);
mContext = context;
super(context, attrs, defStyleAttr);
mContext = context;
if(attrs!=null) {
这个是V7报中的 也可用TypedArray 这里是context.
obtainStyledAttributes 方法也可
TintTypedArray typedArray=TintTypedArray.obtainStyledAttributes(context,attrs,R.styleable.numberAddSubView);
int value=typedArray.getInt(R.styleable.
numberAddSubView_value,
0);
//
默认为
0
if(value> 0) { // 不等于 0 才有值
setValue(value); // 属性如果被赋值,则先执行属性,再执行构造函数,然后是未被赋值的属性初始化
}
int minValue=typedArray.getInt(R.styleable. numberAddSubView_minValue, 0); // 默认为 0
if(minValue> 0) { // 不等于 0 才有值
setMinValue(value); // 属性如果被赋值,则先执行属性,再执行构造函数,然后是未被赋值的属性初始化
}
int maxValue=typedArray.getInt(R.styleable. numberAddSubView_maxValue, 0); // 默认为 0
if(maxValue> 0) { // 不等于 0 才有值
setMaxValue(value); // 属性如果被赋值,则先执行属性,再执行构造函数,然后是未被赋值的属性初始化
}
Drawable numberAddSubView_number_number_background = typedArray.getDrawable(R.styleable. numberAddSubView_number_background);
if( numberAddSubView_number_number_background!= null) {
if (Build.VERSION. SDK_INT >= Build.VERSION_CODES. JELLY_BEAN) {
setBackground( numberAddSubView_number_number_background);
}
}
}
// 把布局添加到类中
View view = View. inflate(context, R.layout. add_sub_number_view, numberAddSubView. this);
btn_sub = (Button) findViewById(R.id. btn_sub);
tv_value = (TextView) findViewById(R.id. tv_value);
btn_add = (Button) findViewById(R.id. btn_add);
btn_sub.setOnClickListener( this);
btn_add.setOnClickListener( this);
}
if(value> 0) { // 不等于 0 才有值
setValue(value); // 属性如果被赋值,则先执行属性,再执行构造函数,然后是未被赋值的属性初始化
}
int minValue=typedArray.getInt(R.styleable. numberAddSubView_minValue, 0); // 默认为 0
if(minValue> 0) { // 不等于 0 才有值
setMinValue(value); // 属性如果被赋值,则先执行属性,再执行构造函数,然后是未被赋值的属性初始化
}
int maxValue=typedArray.getInt(R.styleable. numberAddSubView_maxValue, 0); // 默认为 0
if(maxValue> 0) { // 不等于 0 才有值
setMaxValue(value); // 属性如果被赋值,则先执行属性,再执行构造函数,然后是未被赋值的属性初始化
}
Drawable numberAddSubView_number_number_background = typedArray.getDrawable(R.styleable. numberAddSubView_number_background);
if( numberAddSubView_number_number_background!= null) {
if (Build.VERSION. SDK_INT >= Build.VERSION_CODES. JELLY_BEAN) {
setBackground( numberAddSubView_number_number_background);
}
}
}
// 把布局添加到类中
View view = View. inflate(context, R.layout. add_sub_number_view, numberAddSubView. this);
btn_sub = (Button) findViewById(R.id. btn_sub);
tv_value = (TextView) findViewById(R.id. tv_value);
btn_add = (Button) findViewById(R.id. btn_add);
btn_sub.setOnClickListener( this);
btn_add.setOnClickListener( this);
}