概述
Android中自定义属性,attr中声明的format,即表明其格式。
其中ImageView包含background(背景)和src(前景)属性。
ImageView#attr
View#attr
View.java
case com.android.internal.R.styleable.View_background:
background = a.getDrawable(attr);
break;
ImageView.java
Drawable d = a.getDrawable(com.android.internal.R.styleable.ImageView_src);
if (d != null) {
setImageDrawable(d);
}
reference|color:可以是一个引用drawable的resourceId,也可以是一个color(最终会被封装为ColorDrawable)
attr#format
reference
参考某一资源id,即resourceId
case com.android.internal.R.styleable.View_id:
mID = a.getResourceId(attr, NO_ID);
break;
color
颜色值
case com.android.internal.R.styleable.TextView_textColor:
textColor = a.getColorStateList(attr);
break;
case com.android.internal.R.styleable.TextView_textColorHighlight:
textColorHighlight = a.getColor(attr, textColorHighlight);
break;
boolean
布尔值
final boolean checked = a.getBoolean(
com.android.internal.R.styleable.CompoundButton_checked, false);
setChecked(checked);
dimension
尺寸值
protected void setBaseAttributes(TypedArray a, int widthAttr, int heightAttr) {
width = a.getLayoutDimension(widthAttr, "layout_width");
height = a.getLayoutDimension(heightAttr, "layout_height");
}
float
浮点值
case com.android.internal.R.styleable.View_alpha:
setAlpha(a.getFloat(attr, 1f));
break;
integer
整型值
private static final int ROW_COUNT = R.styleable.GridLayout_rowCount;
private static final int COLUMN_COUNT = R.styleable.GridLayout_columnCount;
string
字符串
case com.android.internal.R.styleable.TextView_text:
text = a.getText(attr);
break;
其中getText和getString,一个返回CharSequence,一个返回String
fraction
百分数,常用于定义animation
enum
枚举值
int index = a.getInt(com.android.internal.R.styleable.LinearLayout_orientation, -1);
if (index >= 0) {
setOrientation(index);
}
flag
位或运算
case com.android.internal.R.styleable.TextView_gravity:
setGravity(a.getInt(attr, -1));
break;
属性定义可以指定多种类型,如background,pivotX等