編輯:關於 Android 程式設計
AttributeSet 是 android.util.AttributeSet 介面,已知間接的子類別為 XmlResourceParser。凡是自定義過 View 的人都會接觸到它——它出現在雙參數與三參數建構子的參數清單中。

AttributeSet 是屬性的集合,通常出現在 XML 文件中。一般來說你不會想直接用它,而是透過能夠為你解析屬性的 Resources.Theme.obtainStyledAttributes() 來操作。
兩者的差別是關鍵所在:Resources API 會自動把資源參照(例如 XML 中的 "@string/my_label")轉換為你想要的型別。如果你直接使用 AttributeSet,就必須手動檢查資源的套用情況(透過 getAttributeResourceValue(int, int)),必要時還得自己去查找資源。直接使用 AttributeSet 另外還有個副作用:它會破壞 Application 的 themes 與 styles 套用——也就是說,寫在 style 中的屬性值不會被套用,只會拿到 XML 上顯式指定的值。
這個介面也提供了一種機制,能透過 XmlPullParser 的 Xml.asAttributeSet() 方法,高效地從編譯過的 .xml 檔中取得資料。通常會回傳一個由 XmlPullParser 介面實作的物件,但它在處理編譯後的 XML 資源時特別有用:
XmlPullParser parser = resources.getXml(myResouce); AttributeSet attributes = Xml.asAttributeSet(parser);
這種實作方式不像 XmlPullParser 那樣是綜合性的。在編譯資源時,它針對 apk 已生成的預算資訊做了檢索上的優化——例如 getAttributeFloatValue(int, float) 回傳的是已儲存在編譯資源中的浮點數,而不是執行期從 .xml 檔中重新解析的原始字串。
此外,這個介面還提供了編譯過的 XML 檔中、原始 XML 檔裡並不存在的附加資訊。例如 getAttributeNameResource(int) 回傳的是與該 XML 相關聯的資源標識符屬性名稱。
getAttributeBooleanValue(String namespace, String attribute, boolean defaultValue)
回傳 attribute 的布林值,找不到時回傳 defaultValue。
getAttributeBooleanValue(int index, boolean defaultValue)
回傳序列號為 index 的 attribute 布林值。
getAttributeCount()
回傳屬性集合中的屬性數量。
getAttributeFloatValue(int index, float defaultValue)/getAttributeFloatValue(String namespace, String attribute, float defaultValue)
回傳浮點數值。
getAttributeIntValue(String namespace, String attribute, int defaultValue)/getAttributeIntValue(int index, int defaultValue)
回傳整數值。
getAttributeListValue(String namespace, String attribute, String[] options, int defaultValue)/getAttributeListValue(int index, String[] options, int defaultValue)
回傳該屬性值在 options 清單中的索引;若屬性不存在或找不到對應項,回傳 defaultValue。這個方法常用於把 XML 中的字串(如 "left"、"center")映射成整數常數。
getAttributeName(int index)
回傳指定屬性的名稱。
getAttributeNameResource(int index)
回傳與指定屬性名稱相關聯的資源 ID,可用於 styles;若無對應資源則回傳 0。注意它與 getAttributeResourceValue(int, int) 不同:前者回傳的是「屬性名稱」的資源標識符,後者回傳的是「屬性值」的資源標識符。
getAttributeResourceValue(String namespace, String attribute, int defaultValue)/getAttributeResourceValue(int index, int defaultValue)
以資源標識符的形式回傳屬性值,也就是原始寫法為 "@package:type/resource" 的那種值。
getAttributeUnsignedIntValue(int index, int defaultValue)/getAttributeUnsignedIntValue(String namespace, String attribute, int defaultValue)
回傳以無號格式表示的整數值,特別處理了 0xn…n 與 #n…n 兩種寫法(十六進位顏色值常見)。
getAttributeValue(int index)/getAttributeValue(String namespace, String name)
以字串形式回傳指定屬性的值。
getClassAttribute()
回傳 "class" 屬性的值,若無則回傳 null。等同於 getAttributeValue(null, "class")。
getIdAttribute()
回傳 "id" 屬性的值,若無則回傳 null。等同於 getAttributeValue(null, "id")。
getIdAttributeResourceValue(int defaultValue)
回傳 "id" 屬性的整數值,若無則回傳 defaultValue。等同於 getAttributeResourceValue(null, "id", defaultValue)。
getPositionDescription()
回傳屬性集合當前位置的描述。例如當屬性集合是從 XML 文件載入時,位置描述可以指示目前的行號——這在除錯「屬性寫在哪一行」時相當實用。
getStyleAttribute()
回傳 "style" 屬性的值,若無則回傳 0。等同於 getAttributeResourceValue(null, "style")。
在自定義 View 中讀取屬性時,優先使用带 AttributeSet 與 defStyle 的 obtainStyledAttributes() 形式:
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.MyView, defStyle, 0); String text = ta.getString(R.styleable.MyView_text); int color = ta.getColor(R.styleable.MyView_color, Color.BLACK); ta.recycle(); // 記得回收
這樣做的好處有三:能正確套用主題與 style 中的預設值、自動完成型別轉換、且不必手動處理資源參照的解析。另外別忘了 recycle()——TypedArray 包裝的是原生資源,未回收會造成資源浪費。
反之,若你只需要簡單的幾個屬性且不在意 style 支援,直接用 AttributeSet 的 getAttributeValue(namespace, name) 也夠用,但要記得自己處理型別轉換與資源查找。
Android Spinner 鍵值對用法:以 Dict 物件搭配 ArrayAdapter
Spinner 最單純的用法是綁定字串陣列,但實務上我們往往需要的不是「字串」,而是「ID + 顯示文字」這樣的鍵值對——例如選擇城市時,畫面上顯示「台北」,送出表單時卻
Android 程式鎖應用簡單實作
程式鎖的常見做法是維護一份已加鎖應用清單,並在列表中提供鎖定與解鎖切換。以下以簡單的列表適配器為例,說明如何在同一畫面管理已加鎖與未加鎖應用。 資料來源 可先建立已加鎖應
動手做一個 ButterKnife:注解與編譯期處理的入門實踐
ButterKnife 幾乎人人用過,最直接的收益就是省下一堆 findViewById。既然原理不複雜,那我們就自己刻一個,取名 BBKnife。 先看 Butter
仿大眾點評:ViewPager + GridView 實現分類橫向滑動
先看效果,再講思路 電商與生活服務類 App 的首頁幾乎都有這麼一塊:一排分類入口,左右滑動換頁,底部有小圓點或分段指示器。實現它不需要什麼高深技術,ViewPager