Android 自定義屬性常用類型
format 常用類型
reference 引用
color 顏色
boolean 布爾值
dimension 尺寸值
float 浮點值
integer 整形值
string 字符串
enum 枚舉
用三種方式從Attributeset取對應的屬性
1.用命名空間取對應的屬性 String
String name = attrs.getAttributeValue("http://schemas.android.com/apk/res/<包名>","屬性名");
2.遍歷式取屬性String 和int
for (int i = 0 ; i < attrs.getAttributeCount();i++){ System.out.println(attrs.getAttributeName(i)+":"+attrs.getAttributeValue(i)); }
3.用系統工具TypedArray取屬性 bitmap String
TypedArray t = context.obtainStyledAttributes(attrs, R.styleable.rainbowbar, 0, 0); hSpace = t.getDimensionPixelSize(R.styleable.rainbowbar_rainbowbar_hspace, hSpace); vSpace = t.getDimensionPixelOffset(R.styleable.rainbowbar_rainbowbar_vspace, vSpace); barColor = t.getColor(R.styleable.rainbowbar_rainbowbar_color, barColor); t.recycle();