自定义组件<二> : TypeArray的用法与介绍

接下来就给大家讲解一下TypeArray的用法:
  TypeArray介绍:
TypedArray typeArray = context.obtainStyledAttributes(attrs,
    R.styleable.titlebar, 0, 0);
TypeArray是一个类型数组,也是一个十分重要的属性容器。用于存放各种属性的资源。
其实例化的时候用context.obtainStyledAttributes来得到。
其中attrs是第一个参数,是一个AttributeSet的类型。
其中第二个是一个int [] attrs,
关于这个:api文档上市这样写的:

Return a StyledAttributes holding the attribute values in set that are listed in attrs. In addition, if the given AttributeSet specifies a style class (through the "style" attribute), that style will be applied on top of the base attributes it defines.

Be sure to call StyledAttributes.recycle() when you are done with the array.

When determining the final value of a particular attribute, there are four inputs that come into play:

其意为:

 返回一个StyledAttributes控股属性值设置在attrs上市。此外,如果给定AttributeSet指定一个样式类(通过“风格”属性),这种风格将被应用的基本属性,它定义了。  一定要调用StyledAttributes.recycle()当你完成数组。  在确定一个特定属性的最终值时,有四个符号是七折作用的:


其中resource是跟标签,可以在里面定义若干个declare-styleable,<declare-styleable name="MyView">中name定义了变量的名称,下面可以再自定义多个属性,针对<attr name="myTextSize" format="dimension"/>来说,其属性的名称为"myTextSize",format指定了该属性类型为dimension,只能表示字体的大小。

format还可以指定其他的类型比如;

reference   表示引用,参考某一资源ID
string   表示字符串
color   表示颜色值
dimension   表示尺寸值
boolean   表示布尔值
integer   表示整型值
float   表示浮点值
fraction   表示百分数
enum   表示枚举值
flag   表示位运算

 最后要调用ReCycle()方法。

注意的是既然是自定义组件,一定要在XML文件中写入:

xmlns:app="http://schemas.android.com/apk/res/com.xxx(包名)"

否则报错的话,就是未写入它引起的。

在下面的环节中,会详细的介绍自定义组件的用法


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

androidstarjack

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值