AttributeSet api分析

(4) 2024-04-18 20:12

Hi,大家好,我是编程小6,很荣幸遇见你,我把这些年在开发过程中遇到的问题或想法写出来,今天说一说AttributeSet api分析,希望能够帮助你!!!。

最近在学习自定义view,在学到view的构造函数的时候需要了解AttributeSet的一些简单用法,这里我通过打断点的方式简单总结下。

下面进行简单分类下

AttributeSet api分析_https://bianchenghao6.com/blog__第1张

1、getAttributeCount() 获取xml文件中view标签上使用的属性数量

2、getAttributeName(index) 获取序号为index的属性的属性名称

AttributeSet api分析_https://bianchenghao6.com/blog__第2张

上面index=0时的属性获取到的是gravity属性,但是gravity属性在xml中并不是排在第一位置的,所以attrs集合中的元素排列并不是按照xml中实际摆放位置排列的,这点了解下。

3、getAttributeValue(index) 获取序号为index的属性对应的属性值

AttributeSet api分析_https://bianchenghao6.com/blog__第3张

4、getAttributeValue(namespace, name) 通过命名空间和属性名,获取属性值

AttributeSet api分析_https://bianchenghao6.com/blog__第4张

AttributeSet api分析_https://bianchenghao6.com/blog__第5张

 5、getAttributeResourceValue(namespace, attribute, defaultvalue)根据命名空间和属性名获取其资源id值;

6、getAttributeResourceValue(int index, int defaultValue) 获取序号为index的属性的资源ID值

        我们知道每个属性在R类中,再定义一个子类,子类中再定义了一个标识符来表示某个属性,属性值是int类型。

         比如R.xxx.attr=0x7f030034。那么getAttributeNameResource(index)返回的就是属性ID的标识符R.xxx.attr; 而getAttributeResourceValue()返回的就是属性id的实际值0x7f030034

 

 7、getPositionDescription() 获取自定义view在xml的行号(感觉没有用) 

AttributeSet api分析_https://bianchenghao6.com/blog__第6张

8、getAttributeNameResource(index)获取序号为index的属性的资源ID标识符,比如R.xxx.xxx

        我们知道每个属性在R类中,再定义一个子类,子类中再定义了一个标识符来表示某个属性,属性值是int类型。

         比如R.xxx.attr=0x7f030034。那么getAttributeNameResource(index)返回的就是属性ID的标识符R.xxx.attr; 而getAttributeResourceValue()返回的就是属性id的实际值0x7f030034

AttributeSet api分析_https://bianchenghao6.com/blog__第7张

AttributeSet api分析_https://bianchenghao6.com/blog__第8张

AttributeSet api分析_https://bianchenghao6.com/blog__第9张

 其中,对2130903095做下进制转换AttributeSet api分析_https://bianchenghao6.com/blog__第10张

9、getIdAttribute() 获取id属性的值(但是不知道为什么这个值一直为mull??)

AttributeSet api分析_https://bianchenghao6.com/blog__第11张

10、getIdAttributeResourceValue() 不知道为啥也获取不到??

AttributeSet api分析_https://bianchenghao6.com/blog__第12张

11、getStyleAttribute() 获取样式属性的值

AttributeSet api分析_https://bianchenghao6.com/blog__第13张 AttributeSet api分析_https://bianchenghao6.com/blog__第14张

其中需要对2131756063做下进制转换

AttributeSet api分析_https://bianchenghao6.com/blog__第15张 12、getClassAttribute()获取class属性的值

 

 

今天的分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。

上一篇

已是最后文章

下一篇

已是最新文章

发表回复