自定View自定义属性详解

开头

先说下自定义View属性的正常使用过程。可以参考本人之前的[自定义View]的笔记。

a. 在values/attrs.xml中添加需要的styleable和item

declare-styleable一般命名成自定义View的名字,多想个名字有时候还是很难的。

declare-styleable其实不写也可以

b. 写一个用到自定义属性的自定义View

c. 在Layout引入命名空间

下面是AS的引入方法。

也可以使用之前一样的方法,如[自定义View]的笔记。

d. 在自定义View布局中添加自定义属性

e. 在自定义View中通过TypedArray获取属性

f. 使用自定义属性

 

自定义属性的format

从上面的步骤看,每个自定义属性都有自己format。

先和所有属性混个脸熟。

  1. String:字符串
  2. Boolean:布尔值
  3. color:颜色值
  4. dimension:尺寸值
  5. enum:枚举类型
  6. flag:位或运算
  7. float:浮点型
  8. fraction:百分比
  9. integer:整型
  10. reference:引用

每个atrr可以使用多个format.

 

format的使用实例

enum

color

flag

fraction

 

reference

引用的是资源的id。

属性的获取

先看一下获取TypedArray的方法。

 

public TypedArray obtainStyledAttributes(AttributeSet set,
       
@StyleableRes int[] attrs, @AttrRes int defStyleAttr, @StyleRes int defStyleRes) {
   
return mThemeImpl.obtainStyledAttributes(this, set, attrs, defStyleAttr, defStyleRes);
}

Return a TypedArray holding the attribute values

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 TypedArray.  Can be
                     0 to not look for defaults.

defStyleRes A resource identifier of a style resource that
                    supplies default values for the TypedArray,
                    used only if defStyleAttr is 0 or can not be found
                    in the theme.  Can be 0 to not look for defaults.

其中属性是从set里面获取的,直接通过set本身,也能获得所有属性名称和值/引用。

attrs就是我们定义在values/attrs.xml里面的属性。

因为set可能是空,所以可以指定默认的styleattr和对应资源。

这里要讲的是直接使用set也绕个弯,也能获取到想要的属性,但比较麻烦。

TyepedArray很好的简化了操作。

TyepedArray用完一定要回收

   通过TypedArray的getXXX方法获取想要属性。

获取属性实例

dimension

可以获取dimension值,也可以直接获取转换为pixel的值。

看实际需要,方法如下

public float getDimension(@StyleableRes int index, float defValue)

public int getDimensionPixelOffset(@StyleableRes int index, int defValue)

public int getDimensionPixelSize(@StyleableRes int index, int defValue)

enum

枚举类型实际就是int值,所以通过getInt获取。

拿到后通过swtich判断具体操作。

flag

同enum用getInt获取。

fraction

a fractional unit attribute

index Index of attribute to retrieve

base The base value of this fraction.  In other words, a
             standard fraction is multiplied by this value.

pbase The parent base value of this fraction.  In other
             words, a parent fraction (nn%p) is multiplied by this
             value.

defValue Value to return if the attribute is not defined or
                 not a resource.

@return Attribute fractional value multiplied by the appropriate
         base value, or defValue if not defined

 

public float getFraction(@StyleableRes int index, int base, int pbase, float defValue)

 

color

reference

获取资源ID

获取后可以像R.xx.xxx一样使用,比这里拿到的是drawable资源

字符传数组ID

直接用getTextArray获取数组

资源数组

获取每个资源的ID,单独获取,这里用字符串做示例

这里要注意因为又一次用到TypedArray,所以用好后一定要回收

猜你喜欢

转载自blog.csdn.net/weixin_39821531/article/details/88741664