`
mmdev
  • 浏览: 12954593 次
  • 性别: Icon_minigender_1
  • 来自: 大连
文章分类
社区版块
存档分类
最新评论

android自定义视图属性(atts.xml,TypedArray)学习

 
阅读更多

最近在学习过程中遇到这个问题,不知道TypedArray是干嘛用的?去官方网站看一下吧:TypedArray继承自Object类,然后再看下它的类概述:



是一个用于存放恢复obtainStyledAttributes(AttributeSet, int[], int, int)或obtainAttributes(AttributeSet, int[])值的一个数组容器,当操作完成以后,一定要调用recycle()方法。用于检索的索引值在这个结构对应的位置给obtainStyledAttributes属性。

使用这个类的时候,先要在valuse文件夹下创建:atts.xml文件:


首先,声明自定义<declare-styleable name="FlowIndicator">,nameFlowIndicator,属性设置为比较简单的格式,前面参数name,后面是参数格式。

自定义属性的format,可以有以下多种:

  • reference
  • string
  • color
  • dimension
  • boolean
  • integer
  • float
  • fraction
  • enum
  • flag

然后这样使用:


最后一定不要忘记typeArray.recycle():

给回以前提取的styledattributes,以后再使用。

应该注意到,获取属性的时候所用的R.styleable.FlowIndicator_count中的FlowIndicator_count是采取的名字_属性这种格式。

定义好了自定义属性,就可以在自定控件中的属性设置了:


首先,要有声明:

xmlns:app="http://schemas.android.com/apk/res/com.dream.myqiyi",“com.dream.myqiyi”这个是你项目的包名。

然后我们就可以使用app:这样设置自定义的属性了。


先简单的写到这吧,如果问题,欢迎探讨。


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics