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

本文详细介绍了TypeArray的概念及其在Android开发中的使用方法。TypeArray作为重要的属性容器,用于存放各种属性资源,并通过context.obtainStyledAttributes获取实例。文章还解释了如何在XML文件中定义自定义属性及类型。
接下来就给大家讲解一下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(包名)"

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

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


### 代码作用 `filterParameter.getType() == XmlRpc::XmlRpcValue::TypeArray` 这行代码主要用于检查 `filterParameter` 这个 `XmlRpc::XmlRpcValue` 对象的类型是否为数组类型。`filterParameter.getType()` 是调用 `XmlRpcValue` 类的 `getType()` 方法,该方法会返回一个表示 `XmlRpcValue` 对象类型的值。`XmlRpc::XmlRpcValue::TypeArray` 是 `XmlRpcValue` 类中定义的一个常量,代表数组类型。如果 `filterParameter` 是数组类型,该表达式返回 `true`,否则返回 `false`。 ### 使用场景 在处理 XML-RPC(一种远程过程调用协议)数据时,经常需要根据数据的类型进行不同的处理。如,当从 XML-RPC 服务器接收到一个 `XmlRpcValue` 对象时,可能不清楚其具体类型,需要先判断它是否为数组类型,再决定如何进一步处理。以下是一个简单的示代码: ```cpp #include <XmlRpc.h> #include <iostream> void processXmlRpcValue(XmlRpc::XmlRpcValue filterParameter) { if (filterParameter.getType() == XmlRpc::XmlRpcValue::TypeArray) { // 处理数组类型的数据 for (int i = 0; i < filterParameter.size(); ++i) { std::cout << "Array element " << i << ": " << filterParameter[i] << std::endl; } } else { // 处理其他类型的数据 std::cout << "Not an array. Value: " << filterParameter << std::endl; } } int main() { XmlRpc::XmlRpcValue arrayValue; arrayValue[0] = 1; arrayValue[1] = 2; arrayValue[2] = 3; processXmlRpcValue(arrayValue); return 0; } ``` ### 可能出现的问题及解决方案 #### 1. 类型判断错误 问题:由于 `XmlRpcValue` 可能有多种类型,可能会因为判断条件不准确导致处理逻辑错误。 解决方案:在进行类型判断时,要确保考虑到所有可能的类型,并进行相应的处理。可以使用 `switch` 语句或者多个 `if-else` 语句来处理不同类型。 #### 2. 空指针异常 问题:如果 `filterParameter` 是一个空指针,调用 `getType()` 方法会导致空指针异常。 解决方案:在调用 `getType()` 方法之前,先检查 `filterParameter` 是否为空指针。示代码如下: ```cpp if (filterParameter.valid()) { if (filterParameter.getType() == XmlRpc::XmlRpcValue::TypeArray) { // 处理数组类型的数据 } else { // 处理其他类型的数据 } } else { std::cout << "filterParameter is not valid." << std::endl; } ``` #### 3. 数组越界访问 问题:在处理数组类型的数据时,如果不小心访问了超出数组范围的元素,会导致数组越界访问错误。 解决方案:在访问数组元素之前,先检查数组的大小,确保访问的索引在有效范围内。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

androidstarjack

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

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

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

打赏作者

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

抵扣说明:

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

余额充值