獲取想要得到的風格設計屬性描述————Resources.Theme類函數public TypedArray obtainStyledAttributes()


今天看到一個群友問一個函數問題,看着挺有用的就研究了下。

看了幫助文檔覺得下面這個method該是最復雜了,能囊括其他幾個了吧。

當然本人初學,只是覺得今天自己算是很花腦子的把這個函數聯系其他的思考了一番,怕以后忘了,應該不完全正確,只是要記錄下用心思考的瞬間。。。

public TypedArray obtainStyledAttributes(AttributeSet set, int[] attrs, int defStyleAttr, int defStyleRes)

照文檔翻譯是,返回一個設計樣式屬性包含了set里面的attrs參數:

AttributeSet 是一個由資源xml文件獲得的各屬性接口類(具體還有待研究,只是看文檔下的代碼推敲的)

XmlPullParser parser = resources.getXml(myResouce);
AttributeSet attributes = Xml.asAttributeSet(parser);

而int[] attrs 描述里是指定自己想要獲取的屬性項。所以我覺得大意是獲取AttributeSet類成員里的attrs指定的成員。

 

接着是提取屬性的優先級:

When determining the final value of a particular attribute, there are four inputs that come into play:

  1. Any attribute values in the given AttributeSet.    
  2. The style resource specified in the AttributeSet (named     "style").    
  3. The default style specified by defStyleAttr and     defStyleRes
  4. The base values in this theme.

第一優先級:AttributeSet里指定的屬性

。二。。。:在AttributeSet里指定的名為"style"的風格資源(這個和第一點的區別不知道是不是整體和個別的區別以后用到的再研究下)

。三。。。:由參數defStyleAttr和defStyleRes指定的默認屬性(PS:這樣設計或許是為了可以自定義一個接口名在這以后想替換的時候把前面兩個參數補全就行)

。四。。。:主題默認

例如你在AttributeSet里設置了 <Button textColor="#ff000000">,則此Button文字只會是黑色而不管其他風格里怎么設計。

返回一個TypedArray,該是存儲屬性的數組。當使用它時必須調用TypedArray.recycle()

Parameters
set         The base set of attribute values.  May be null.
attrs         The desired attributes to be retrieved.
defStyleAttr    An attribute in the current theme that contains a reference to a style resource that supplies defaults values for the StyledAttributes.  Can be 0 to not look for defaults.當前主題的默認風格屬性來自一個風格設計資源的引用。能為0或者不再尋找默認(上面優先級的第四個)

defStyleRes    A resource identifier of a style resource that supplies default values for the StyledAttributes,used only if defStyleAttr is 0 or can not be found in the theme.  Can be 0 to not look for defaults.一個風格類資源的ID,只當defStyleAttr為0或者找不到時使用.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM