一、attributeset概述
1、attributeset是Android中一個非常重要的類,它是View中的一個成員變數,用於存儲所有的屬性值。attributeset中包含了我們在xml中聲明的所有屬性,通過解析xml,系統將其綁定在View上。此外,attributeset還是自定義View中非常重要的一個類。
2、attributeset可以通過三種方式設置:
// 從資源中解析attributes mContext.obtainStyledAttributes(attrs, R.styleable.MyView); // 從主題中解析attributes mContext.getTheme().obtainStyledAttributes(attrs, R.styleable.MyView, 0, 0); // 在java代碼中直接新建attributes AttributeSet as = new AttributeSet();
3、在編寫自定義View時,我們需要重寫view的三個構造方法,其中第一個和第二個方法都是通過調用第三個方法實現的。第三個方法中會傳入一個attributeset類型的參數,這個attributeset對象保存了從xml文件中解析出來的屬性值。
public class MyView extends View {
public MyView(Context context) {
this(context, null);
}
public MyView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
}
二、attributeset attrs詳解
1、在attributeset中attrs是一個數組,包含了我們在xml文件中定義的所有屬性。在自定義view中,我們可以通過使用TypedArray自定義獲取每一個屬性值。
TypedArray a = mContext.obtainStyledAttributes(attrs, R.styleable.MyView); int textColor = a.getColor(R.styleable.MyView_textColor, Color.BLACK); float textSize = a.getDimension(R.styleable.MyView_textSize, 12); String text = a.getString(R.styleable.MyView_text);
2、在xml文件中定義的屬性可以分為以下三類:
(1)系統自帶的屬性,如android:layout_width,android:layout_height等;
(2)自定義的屬性,在values/attrs.xml文件中定義,通過<declare-styleable>標籤進行聲明,<attr name=”xxxx” format=”yyyy”/>用來定義屬性;
(3)繼承自系統或其他庫的屬性。
3、在定義自定義屬性時,可以通過format屬性來定義屬性值的類型,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="MyView">
<attr name="text" format="string" />
<attr name="textSize" format="dimension" />
<attr name="textColor" format="color" />
<attr name="isBold" format="boolean" />
<attr name="icon" format="reference" />
</declare-styleable>
</resources>
三、attributeset類詳解
1、attributeset類是一個介面,用於描述一個xml節點中的一組屬性值。介面中包含了獲取屬性值的一系列方法。
public interface AttributeSet {
int getAttributeCount();
String getAttributeName(int index);
String getAttributeValue(int index);
String getAttributeValue(String namespace, String name);
String getPositionDescription();
int getAttributeNameResource(int index);
int getAttributeListValue(String namespace, String attribute, String[] options, int defaultValue);
boolean getAttributeBooleanValue(String namespace, String attribute, boolean defaultValue);
int getAttributeResourceValue(String namespace, String attribute, int defaultValue);
float getAttributeFloatValue(String namespace, String attribute, float defaultValue);
int getAttributeIntValue(String namespace, String attribute, int defaultValue);
int getAttributeUnsignedIntValue(String namespace, String attribute, int defaultValue);
int getAttributeListValue(int index, String[] options, int defaultValue);
boolean getAttributeBooleanValue(int index, boolean defaultValue);
int getAttributeResourceValue(int index, int defaultValue);
float getAttributeFloatValue(int index, float defaultValue);
int getAttributeIntValue(int index, int defaultValue);
int getAttributeUnsignedIntValue(int index, int defaultValue);
String getIdAttribute();
}
2、在使用attributeset類時應該注意:
(1)getAttributeValue(String namespace, String name)在部分系統版本上存在問題,建議使用getAttributeValue(int index)
(2)getAttributeBooleanValue方法獲取屬性值是根據字元串值進行判斷的,如果字元串值不是”true”或者”false”則會默認返回defaultValue。
四、總結
1、attributeset是android中非常重要的一個類,它用於存儲View中的所有屬性。一般情況下,我們不需要直接操作attributeset類,而是通過TypedArray類進行獲取屬性值。
2、在自定義View中,通過定義自定義屬性可以使我們的View使用起來更加方便。
3、當使用attributeset類進行屬性值獲取時,需要注意一些細節問題。
4、在實際開發中,attributeset類會頻繁使用,掌握其使用方法和特性是android開發者必備的技能之一。
原創文章,作者:WFQO,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/143427.html
微信掃一掃
支付寶掃一掃