android自定义标签属性
来源:互联网 发布:数据库注册码 编辑:程序博客网 时间:2024/05/17 03:38
Android自定义标签属性
参考资料
- Valid format values for declare-styleable/attr tags
- Why do you have to recycle a TypedArray
实例代码
首先在attrs.xml文件中添加声明
<declare-styleable name="HeaderView"> <attr name="header_title" format="string" /> <attr name="header_left" format="reference" /> <attr name="header_right" format="reference" /> <attr name="header_right_text" format="string" /> <attr name="header_center" format="reference"/> <attr name="header_center_type" format="string"/></declare-styleable>
然后在布局xml文件中使用
<com.bee.widget.HeaderView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:hv="http://schemas.android.com/apk/res-auto" android:id="@+id/hv_date_record" android:layout_width="match_parent" android:layout_height="50dp" android:background="@color/bg_headerview" hv:header_center_type="title" hv:header_center="@string/date_record_title" hv:header_left="@drawable/headerview_back" hv:header_right="@drawable/headerview_message" />
最后在自定义组件中获取TypedArray, 获取从xml传递的数据
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.HeaderView, 0, 0);int centerTitleId = a.getResourceId(R.styleable.HeaderView_header_center, -1);int left = a.getResourceId(R.styleable.HeaderView_header_left, -1);int right = a.getResourceId(R.styleable.HeaderView_header_right, -1);int center = a.getResourceId(R.styleable.HeaderView_header_center, -1);
- 注意:xmlns:hv=”http://schemas.android.com/apk/res-auto” 可自动获取
attrs.xml中的format
- reference
- string
- color
- dimension
- boolean
- integer
- float
- fraction
- enum
- flag
如果不指定format属性,则默认为reference
TypedArray
- Container for an array of values that were retrieved with obtainStyledAttributes(AttributeSet, int[], int, int) or obtainAttributes(AttributeSet, int[]). Be sure to call recycle() when done with them. The indices used to retrieve values from this structure correspond to the positions of the attributes given to obtainStyledAttributes.
TypedArray类似于键值对的map
可以使用TypedArray代替ViewHolder对ListView, GridView的Adapter的getView方法进行优化
0 0
- android自定义标签属性
- android 自定义标签属性设置
- Android 自定义标签属性设置及使用
- Android自定义属性,<attr>标签format属性值
- 自定义HTML标签属性
- HTML 标签自定义属性
- HTML标签自定义属性
- html自定义标签属性
- 自定义标签的属性
- 自定义HTML标签属性
- HTML标签自定义属性
- HTML标签自定义属性
- HTML标签自定义属性
- 自定义动态属性标签
- HTML标签自定义属性
- HTML标签自定义属性
- JavaScript 标签自定义属性
- 自定义HTML标签属性
- 二维数组的在函数中的传递与使用
- XML数据的三种解析(JSON方式 , DOM方式 , Sax方式)
- Windows下搭建PHP开发环境
- Linux与Windows文件共享
- Codeforces #264(div 2)D.Gargari and Permutations
- android自定义标签属性
- volatile
- Windows应用程序如何找到DLL文件
- 关于VSS2005不能添加目录的解决办法
- apache与nginx
- poj 1160 Post Office(四边形不等式优化dp)
- 网络数据请求(同步/异步连接下的GET/POST请求)
- LIB和DLL的区别与使用
- 深入解析单例线程安全问题