android自定义属性的使用
2016-04-12 15:32
411 查看
自定义属性参数介绍(属性定义可以多种同时使用)
reference:参考某一资源ID。
color:颜色值。
boolean:布尔值。
dimension:尺寸值。
float:浮点值。
integer:整型值。
string:字符串。
fraction:百分数。
enum:枚举值。
flag:位或运算。
以上属性在attrs文件下定义
在xml中使用这些自定义属性:
在头部添加 xmlns:attrstest=”http://schemas.android.com/apk/res-auto” (attrstest这个名称可以随意命名)
代码中获取自定义属性的值:
通过obtainStyledAttributes方法得到TypedArray,注意最后记得回收这个TypedArray。代码如下:
以上是最基本的介绍,更多功能后续探讨
reference:参考某一资源ID。
color:颜色值。
boolean:布尔值。
dimension:尺寸值。
float:浮点值。
integer:整型值。
string:字符串。
fraction:百分数。
enum:枚举值。
flag:位或运算。
以上属性在attrs文件下定义
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="MyTextView"> <attr name="header" format="reference"/> <attr name="headerHeight" format="dimension"/> <attr name="headerVisibleHeight" format="dimension"/> <attr name="age"> <flag name="child" value="10"/> <flag name="young" value="18"/> <flag name="old" value="60"/> </attr> </declare-styleable> </resources>
在xml中使用这些自定义属性:
在头部添加 xmlns:attrstest=”http://schemas.android.com/apk/res-auto” (attrstest这个名称可以随意命名)
代码中获取自定义属性的值:
通过obtainStyledAttributes方法得到TypedArray,注意最后记得回收这个TypedArray。代码如下:
TypedArray types = context.obtainStyledAttributes(attrs, R.styleable.MyTextView); int count = attrs.getAttributeCount(); for(int i = 0;i < count;i++){ int attr = types.getIndex(i); switch (attr){ case R.styleable.MyTextView_header: break; case R.styleable.MyTextView_headerHeight: float headerHeight = types.getDimension(attr,-1); break; case R.styleable.MyTextView_headerVisibleHeight: break; case R.styleable.MyTextView_age: break; } } types.recycle();
以上是最基本的介绍,更多功能后续探讨
相关文章推荐
- jquery 获取自定义属性(attr和prop)的实现代码
- 使用jQuery获取data-的自定义属性
- 理解Android中的自定义属性
- Android重写View并自定义属性实例分析
- 自定义view的自定义属性的引用
- declare-styleable的使用
- HTML5 data-* 自定义属性
- android中attrs自定义属性详解
- Android自定义属性
- Android 自定义属性TypedArray
- 图片与屏幕尺寸不匹配
- Android 自定义属性
- Android 深入理解Android中的自定义属性
- Android 自定义属性的使用
- android自定义View之滑动开关SlideButton
- Android自定义属性以及组合View
- 布局中自定义属性使用模板
- Android在xml布局中找不到自定义属性的问题
- TypedArray和obtainStyledAttributes的使用
- Js自定义属性