Android_TypedArray和obtainStyledAttributes使用

本文深入探讨了自定义属性的格式及其在TypedArray实例中的应用,包括多种属性类型如reference、string、color等,并详细解释了context.obtainStyledAttributes方法的使用。文章进一步阐述了属性的优先级规则,从直接指定属性值到通过style资源和默认样式属性的综合考量。同时,提供了XML和style定义示例,帮助开发者更好地理解和应用这些概念。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值