【Android 自定義 View】之自定義屬性相關(guān)

今天總結(jié)一下自定義 View 時自定義屬性的相關(guān)。

定義

在 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 標(biāo)簽定義一個屬性,name 是屬性的名字,不能和其它屬性的名字沖突,format 是屬性的格式,可以用給一個屬性指定多種格式;
  • declare-styleable 標(biāo)簽定義一個屬性組,可以在里面定義屬性,也可以直接引用 resource 標(biāo)簽下面定義的屬性,區(qū)別就是不用寫 format;

格式

自定義屬性共有10種 format

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

獲取

屬性的獲取有兩種方式:

獲取 resource 下的 attr

第一種是直接獲取 resource 標(biāo)簽下定義的 attr。
每一個 attr 都會在 R 文件的 attr 類中生成一個對應(yīng) ID,我們可以根據(jù)這個 ID 獲取自己定義的屬性,也可以獲取系統(tǒng)定義的屬性。
通常是在 View 的構(gòu)造方法中獲?。?/p>

    //獲取自定義屬性
    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 屬性集中的屬性。
當(dāng)定義 declare-styleable 時,R 文件在 styleable 內(nèi)部類中自動生成一個 int[] 常量,數(shù)組中的元素是 declare-styleable 屬性集中的屬性的 ID,這樣就不需要自己再定義 int[] 了,數(shù)組中的每一個元素也會生成一個 ID 指向數(shù)組中的元素,ID 格式為:屬性集名_屬性名,直接通過屬性 ID 獲取對應(yīng) 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

上面獲取屬性的方式是調(diào)用了 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)

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

obtainStyledAttributes(int[] attrs)

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

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

那么在應(yīng)用的主題中:

<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 中獲取,參數(shù) resid 就是 style 在 R 文件中生成的 ID,我們需要在定義的 style 中聲明相應(yīng)的屬性值:

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

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

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

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

TypedArray

Context 類的 obtainStyledAttributes 方法實質(zhì)調(diào)用的都是 Resources 類的內(nèi)部類 Theme 類的對應(yīng)的 obtainStyledAttributes 方法,最終調(diào)用的都是 ResourcesImpl.ThemeImpl 類的 obtainStyledAttributes 方法,這里不作討論,該方法返回的是一個 TypedArray 對象,TypedArray 類就是一個獲取到的屬性值數(shù)組的容器,該類提供了一系列獲取屬性值的方法:

//獲取屬性的數(shù)量
public int getIndexCount()
//獲取屬性名
public int getIndex (int at)
//獲取屬性類型
public int getType (int index)
//在使用之后要進(jìn)行回收才能重用
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)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容