TypedArray:在自定义布局文件时
TypedArray:存储资源数组的容器;
1.这一种也是我个人借阅他人;它是通过上下文获取布局文件与attrs属性;然后getxxx()获取绑定到对应的控件;
TypedArray _TypedArray = mContext.getTheme().obtainStyledAttributes(attrs, R.styleable.MyFirstCustomerView, 0, 0);
try {
mText = _TypedArray.getString(R.styleable.MyFirstCustomerView_text);
mTextColor = _TypedArray.getColor(R.styleable.MyFirstCustomerView_textColor, Color.BLACK);
mTextSize = _TypedArray.getDimensionPixelSize(R.styleable.MyFirstCustomerView_textSize, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 16, getResources().getDisplayMetrics()));
}
finally {
TypedArray _TypedArray.recycle();
}
2.可以通过obtaiStyledAttributes()方法创建出来;如果不在使用了,请注意调用recycle()方法将它释放;
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.inputView);
inputIcon = typedArray.getResourceId(R.styleable.inputView_input_icon,R.drawable.myacc);
inputHint = typedArray.getNonResourceString(R.styleable.inputView_input_hint);
isPassword = typedArray.getBoolean(R.styleable.inputView_is_password,false);
getResourceId(调用该布局文件与attrs里面存放的属性匹配存储)布局文件没有设置该对应的属性时候,会按照第二个默认值或已有的值属性;