Android_TypedArray和obtainStyledAttributes使用

TypedArray实例是个属性的容器,context.obtainStyledAttributes()方法返回得到。AttributeSet是节点的属性集合.

这里的自定义属性的format,可以有很多种:

  • reference
  • string
  • color
  • dimension
  • boolean
  • integer
  • float
  • fraction
  • enum
  • flag  

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

返回一个设计样式属性包含了set里面的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:这样设计或许是为了可以自定义一个接口名在这以后想替换的时候把前面两个参数补全就行)

。四。。。:主题默认

xml里的显示定义如   bar:attr1="12345" > xml里的style定义如:android:style=@style/test > 当前theme > 备用Style。

原文地址:https://www.cnblogs.com/wangziqiang/p/4370232.html