context.obtainStyledAttributes 研究
我们在自定义View 时,一般都会用到
TypedArray obtainStyledAttributes(
AttributeSet set, int[] attrs, int defStyleAttr, int defStyleRes); 对于它的前面2个参数可能大家都知道, 对于后面两个参数一直不明白怎么用。
于是今天查了下。
context obtainStyledAttributes 最终会调到Resources.Theme.obtainStyledAttributes(AttributeSet set,
int[] attrs, int defStyleAttr, int defStyleRes)
再看一下参数解释:
* @param set The base set of attribute values. May be null.
* @param attrs The desired attributes to be retrieved.
* @param 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.
* @param 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.
defStyleAttr 指向当前theme 某个item 描述的style 该style指定了一些默认值为这个TypedArray
defStyleRes 当defStyleAttr 找不到或者为0, 可以直接指定某个style
不是很理解 ,搜了一下源码:
发现用的地方 不多。如CalendarView
TypedArray attributesArray = context.obtainStyledAttributes(attrs, R.styleable.CalendarView,
R.attr.calendarViewStyle, 0);
CalendarView 在 attrs.xml 中 定义如下:
默认 defStyleAttr 为 calendarViewStyle
也定义在attrs.xml
再看看在theme.xml 中 它指向:
更多相关文章
- android自定义注解的运用
- android标题栏中添加返回按钮
- Android(安卓)ListView的OnItemClickListener()参数详解
- Android(安卓)popupWindow 用法
- android 实现男女按钮选择
- Android(安卓)模糊搜索rawquery bind or column index out of ra
- Android中自定义ScrollView代码实例
- Android笔记(1)adb的使用
- Android摄像头采集Demo