【Android 自定義 View】之自定義屬性相關

今天總結一下自定義 View 時自定義屬性的相關。

定義

在 res/values 中創(chuàng)建 attrs.xml 文件。
示例代碼:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="custom_color" format="color"/>

    <declare-styleable name="CustomAttribute">
        <attr name="custom_radius" format="dimension"/>
        <attr name="custom_color"/>
    </declare-styleable>
</resources>
  • attr 標簽定義一個屬性,name 是屬性的名字,不能和其它屬性的名字沖突,format 是屬性的格式,可以用給一個屬性指定多種格式;
  • declare-styleable 標簽定義一個屬性組,可以在里面定義屬性,也可以直接引用 resource 標簽下面定義的屬性,區(qū)別就是不用寫 format;

格式

自定義屬性共有10種 format

  1. integer 整型值;
  2. float 浮點值;
  3. string 字符串;
  4. boolean 布爾值;
  5. dimension 尺寸值;
  6. color 顏色值;
  7. fraction 百分數;
  8. enum 枚舉值;
  9. flag 位或運算
  10. reference 引用資源 ID

獲取

屬性的獲取有兩種方式:

獲取 resource 下的 attr

第一種是直接獲取 resource 標簽下定義的 attr。
每一個 attr 都會在 R 文件的 attr 類中生成一個對應 ID,我們可以根據這個 ID 獲取自己定義的屬性,也可以獲取系統(tǒng)定義的屬性。
通常是在 View 的構造方法中獲取:

    //獲取自定義屬性
    int[] customAttrs = {R.attr.custom_color};
    TypedArray a = context.obtainStyledAttributes(attrs, customAttrs);
    int color = a.getColor(0, Color.WHITE);
    a.recycle();
    //獲取系統(tǒng)定義屬性
    int[] customAttrs = {android.R.attr.color};
    TypedArray a = context.obtainStyledAttributes(attrs, customAttrs);
    mColor = a.getColor(0, mColor);
    a.recycle();

獲取 declare-styleable 下的 attr

更常用的是獲取 declare-styleable 屬性集中的屬性。
當定義 declare-styleable 時,R 文件在 styleable 內部類中自動生成一個 int[] 常量,數組中的元素是 declare-styleable 屬性集中的屬性的 ID,這樣就不需要自己再定義 int[] 了,數組中的每一個元素也會生成一個 ID 指向數組中的元素,ID 格式為:屬性集名_屬性名,直接通過屬性 ID 獲取對應 attr。

TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomAttribute);
    mColor = a.getColor(R.styleable.CustomAttribute_custom_color, mColor);
    mRadius = a.getDimension(R.styleable.CustomAttribute_custom_radius, mRadius);
    a.recycle();

也可以獲取系統(tǒng)中定義的屬性,這里 android:color 變成了 android_color

//把系統(tǒng)定義的屬性放在屬性集中
<declare-styleable name="CustomAttribute">
    <attr name="android:color"/>
</declare-styleable>
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomAttribute);
mColor = a.getColor(R.styleable.CustomAttribute_android_color, mColor);
a.recycle();

Context#obtainStyledAttributes

上面獲取屬性的方式是調用了 Context 類的 obtainStyledAttributes 方法,該方法有四個重載方法:

//從 Theme 中獲取屬性
obtainStyledAttributes(int[] attrs)
//從 style 中獲取屬性
obtainStyledAttributes(int resid, int[] attrs)
//從 layout 文件中獲取屬性
obtainStyledAttributes(AttributeSet set, int[] attrs)
//從 layout 文件中獲取屬性
obtainStyledAttributes(AttributeSet set, int[] attrs, int defStyleAttr,int defStyleRes)

參數中的 int[] attrs 就是要獲取的屬性的名稱,前面說過這個參數一般為 declare-styleable 在 R 文件的 styleable 類中生成的 ID。

obtainStyledAttributes(int[] attrs)

一個參數的方法是從應用的主題中獲取屬性,如果想從主題中獲取我們自定義的屬性,就需要在應用的主題中聲明自定義的屬性值。
加入定義了如下屬性:

<declare-styleable name="CustomAttribute">
    <attr name="custom_color" format="color"/>
    <attr name="custom_radius" format="dimension"/>
</declare-styleable>

那么在應用的主題中:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!--直接在主題中指定-->
    <item name="custom_color">#FF0000</item>
    <item name="custom_radius">100dp</item>
</style>
obtainStyledAttributes(int resid, int[] attrs)

該方法是從指定的 style 中獲取,參數 resid 就是 style 在 R 文件中生成的 ID,我們需要在定義的 style 中聲明相應的屬性值:

<style name="CustomTheme">
    <item name="custom_color">#00FF00</item>
    <item name="custom_radius">10dp</item>
</style>
obtainStyledAttributes(AttributeSet set, int[] attrs)

該方法是最常用的,是從 layout 文件中獲取,AttributeSet 類型的參數是從 xml 文件中解析出來的控件屬性集合,也包括控件應用的 style 中聲明的屬性。

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

第一個參數是從 xml 文件中解析出來的控件屬性集合,第三個參數是 style.xml 文件中定義的某一 Theme 的 ID,第四個參數是 style.xml 中定義的某一 Style 的 ID。
該方法首先從第一個參數 AttributeSet 也就是 layout 文件中獲取屬性,如果沒有取到屬性,那么從第三個參數 defStyleAttr 指定的主題中獲取,如果還沒有取到,那么從第四個參數 defStyleRes 指定的 style 中獲取,如果還沒有獲取到,那么返回的結果就為 null 了。

TypedArray

Context 類的 obtainStyledAttributes 方法實質調用的都是 Resources 類的內部類 Theme 類的對應的 obtainStyledAttributes 方法,最終調用的都是 ResourcesImpl.ThemeImpl 類的 obtainStyledAttributes 方法,這里不作討論,該方法返回的是一個 TypedArray 對象,TypedArray 類就是一個獲取到的屬性值數組的容器,該類提供了一系列獲取屬性值的方法:

//獲取屬性的數量
public int getIndexCount()
//獲取屬性名
public int getIndex (int at)
//獲取屬性類型
public int getType (int index)
//在使用之后要進行回收才能重用
public void recycle ()

public boolean getBoolean (int index, boolean defValue)
public int getColor (int index, int defValue)
publicColorStateList getColorStateList(int index)
public float getDimension (int index, float defValue)
public int getDimensionPixelOffset (int index, int defValue)
public int getDimensionPixelSize (int index, int defValue)
publicDrawable getDrawable(int index)
public float getFloat (int index, float defValue)
public float getFraction (int index, int base, int pbase, float defValue)
public int getInt (int index, int defValue)
public int getInteger (int index, int defValue)
public intgetLayoutDimension(int index,Stringname)
public int getResourceId (int index, int defValue)
publicString (int index)
publicCharSequencegetText(int index)
publicCharSequence[]getTextArray(int index)

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,030評論 6 531
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 98,310評論 3 415
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 175,951評論 0 373
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,796評論 1 309
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,566評論 6 407
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 55,055評論 1 322
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,142評論 3 440
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,303評論 0 288
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發(fā)現(xiàn)了一具尸體,經...
    沈念sama閱讀 48,799評論 1 333
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,683評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,899評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,409評論 5 358
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 44,135評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,520評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,757評論 1 282
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,528評論 3 390
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,844評論 2 372

推薦閱讀更多精彩內容