attr全稱attribute,意思是屬性,性質(zhì)的意思(個人理解),Android中的attr文件用于描述自定義view的自定義屬性
安卓自帶的attr屬性有常見的比如view的layout_width,id,text都是在attr里描述好的屬性。

安卓sdk中attr描述(部分示例)
<declare-styleable name="TextView">
<!-- Determines the minimum type that getText() will return.
The default is "normal".
Note that EditText and LogTextBox always return Editable,
even if you specify something less powerful here. -->
<attr name="bufferType">
<!-- Can return any CharSequence, possibly a
Spanned one if the source text was Spanned. -->
<enum name="normal" value="0" />
<!-- Can only return Spannable. -->
<enum name="spannable" value="1" />
<!-- Can only return Spannable and Editable. -->
<enum name="editable" value="2" />
</attr>
<!-- Text to display. -->
<attr name="text" format="string" localization="suggested" />
<!-- Hint text to display when the text is empty. -->
<attr name="hint" format="string" />
<!-- Text color. -->
<attr name="textColor" />
</declare-styleable>
看過原生的定義了,下面介紹我們?nèi)绾巫远xattr
1 <declare-styleable name="MyView">
2 <attr name ="屬性1" format = "這個屬性的取值格式">
3 <enum name="取值1" value="程序中對應(yīng)的值"/>
<enum name="取值1" value="程序中對應(yīng)的值"/>
<enum name="取值1" value="程序中對應(yīng)的值"/>
<enum name="取值1" value="程序中對應(yīng)的值"/>
4 <flag name="取值1" value="程序中對應(yīng)的值" />
<flag name="取值2" value="程序中對應(yīng)的值" />
<flag name="取值3" value="程序中對應(yīng)的值" />
</declare-styleable>
聲明一個MyView的屬性組,屬性組下面有很多屬性attr,可以用enum,和flag為attr提供默認值選項(可以不填)。
下面介紹一個重要的東西,attr的格式——format:

format的選項
- fraction:百分比
- reference: 資源引用
剩下的看字面意思以此類推了。
如果想引用attr的值賦值給別的屬性:
android:textColor="?attr/qmui_config_color_gray_5"
android:textSize="?attr/qmui_common_list_item_detail_h_text_size"
注意attr值在此之前一定被賦值過,才能賦值給別人。
attr值解析
被解析attr示例:
<declare-styleable name="roundedimageview">
<attr name="border_thickness" format="" />
<attr name="border_inside_color" format="color" />
<attr name="border_outside_color" format="color"/>
<attr name="border_radius" format="dimension"/>
</declare-styleable>
解析模板
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.屬性組名稱, defStyleAttr, defStyleRes);
typedArray .getXX(R.styleable.XX_xxx);
typedArray .recycle();
解析示例
// 這個attr參數(shù)是構(gòu)造方法傳來的
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CircularCoverView);
leftTopRadians = typedArray.getDimensionPixelSize(R.styleable.CircularCoverView_left_top_radius, leftTopRadians);
leftBottomRadians = typedArray.getDimensionPixelSize(R.styleable.CircularCoverView_left_bottom_radius, leftBottomRadians);
rightTopRadians = typedArray.getDimensionPixelSize(R.styleable.CircularCoverView_right_top_radius, rightTopRadians);
rightBottomRadians = typedArray.getDimensionPixelSize(R.styleable.CircularCoverView_right_bottom_radius, rightBottomRadians);
coverColor = typedArray.getColor(R.styleable.CircularCoverView_cover_color, coverColor);
typedArray.recycle();
obtainStyledAttributes()用來獲得一個TypedArray(一個存放attr屬性的數(shù)組)

obtainStyledAttributes()
- set:一個和xml中的標(biāo)簽關(guān)聯(lián)的存放屬性的集合
- attrs:我們要在xml中讀取的屬性
- defStyleAttr:當(dāng)前主題中的一個屬性,其中包含對為TypedArray提供默認值的樣式資源的引用。可以為0以不尋找默認值。
- defStyleRes:是具體的style資源。為TypedArray提供默認值的樣式資源的資源標(biāo)識符,僅當(dāng)defStyleAttr為0或在主題中找不到時才使用。 可以為0以不尋找默認值。
其中set和defStyle可以使用構(gòu)造傳來的參數(shù),attr使用我們自定義的attr屬性。