android 之 Attr的使用
来源:互联网 发布:摄影师怎么接淘宝单子 编辑:程序博客网 时间:2024/06/03 16:01
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.是一个用于存放恢复obtainStyledAttributes(AttributeSet, int[], int, int)或
obtainAttributes(AttributeSet, int[])
值的一个数组容器,当操作完成以后,一定要调用recycle()方法。用于检索的索引值在这个结构对应的位置给obtainStyledAttributes属性。使用这个类的时候,先要在valuse文件夹下创建:atts.xml文件:
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <declare-styleable name="FlowIndicator">
- <attr name="count" format="integer" />
- <attr name="space" format="dimension" />
- <attr name="point_size" format="dimension" />
- <attr name="point_seleted_color" format="color|reference" />
- <attr name="point_normal_color" format="color|reference" />
- <attr name="point_radius" format="dimension" />
- </declare-styleable>
- </resources>
自定义属性的format,可以有以下多种:
- reference
- string
- color
- dimension
- boolean
- integer
- float
- fraction
- enum
- flag
然后这样使用:
- public FlowIndicator(Context context, AttributeSet attrs) {
- super(context, attrs);
- //获得实例
- TypedArray typeArray = context.obtainStyledAttributes(attrs,
- R.styleable.FlowIndicator);
- //从typeArray获取相应值,第二个参数为默认值,如第一个参数在atts.xml中没有定义,返回第二个参数值
- count = typeArray.getInteger(R.styleable.FlowIndicator_count, 4);
- space = typeArray.getDimension(R.styleable.FlowIndicator_space, 9);
- radius = typeArray.getDimension(R.styleable.FlowIndicator_point_radius, 9);
- point_normal_color = typeArray.getColor(
- R.styleable.FlowIndicator_point_normal_color, 0x000000);
- point_seleted_color = typeArray.getColor(
- R.styleable.FlowIndicator_point_seleted_color, 0xffff07);
- int sum = attrs.getAttributeCount();
- if (Constans.DEBUG) {
- String str = "";
- for (int i = 0; i < sum; i++) {
- String name = attrs.getAttributeName(i);
- String value = attrs.getAttributeValue(i);
- str += "attr_name :" + name + ": " + value + "\n";
- }
- Log.i("attribute", str);
- }
- typeArray.recycle();
- }
最后一定不要忘记typeArray.recycle():
- Give back a previously retrieved StyledAttributes, for later re-use.
反回以前提取的styledattributes,以后再使用。
在获取属性值的时候所用类似R.styleable.FlowIndicator_count的方式,其中FlowIndicator_count是采取的名字_属性这种格式。定义好了自定义属性,就可以在自定控件中的属性设置了:
- <?xml version="1.0" encoding="utf-8"?>
- <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res/com.dream.myqiyi"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <com.dream.myqiyi.widget.FlowIndicator
- android:id="@+id/myView"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_marginBottom="5dip"
- app:count="4"
- android:gravity="center"
- app:point_normal_color="#45000000"
- app:point_radius="3dip"
- app:point_seleted_color="#ffffff"
- app:point_size="5dip"
- app:space="10dip" />
- </FrameLayout>
然后我们就可以使用app:这样设置自定义的属性了。
如果控件继承自某个View类,那么在定义属性的时候就不用考虑布局的属性的了;
0 0
- android 之 Attr的使用
- Android高手之路之Android中的自定义属性attr.xml、TypedArray的使用
- Android高手进阶教程(四)之----Android 中自定义属性(attr.xml,TypedArray)的使用!
- Android高手进阶教程(四)之 ----Android 中自定义属性(attr.xml,TypedArray)的使用
- Android高手进阶教程(四)之----Android 中自定义属性(attr.xml,TypedArray)的使用!
- Android高手进阶教程(四)之----Android 中自定义属性(attr.xml,TypedArray)的使用!
- Android高手进阶教程(四)之----Android 中自定义属性(attr.xml,TypedArray)的使用!
- Android高手进阶教程(四)之----Android 中自定义属性(attr.xml,TypedArray)的使用!
- Android高手进阶教程(四)之----Android 中自定义属性(attr.xml,TypedArray)的使用!
- Android高手进阶教程(四)之----Android 中自定义属性(attr.xml,TypedArray)的使用!
- Android高手进阶教程(四)之----Android 中自定义属性(attr.xml,TypedArray)的使用!
- Android高手进阶教程(四)之----Android 中自定义属性(attr.xml,TypedArray)的使用!
- Android高手进阶教程(四)之----Android 中自定义属性(attr.xml,TypedArray)的使用!
- Android高手进阶教程(四)之----Android 中自定义属性(attr.xml,TypedArray)的使用!
- Android高手进阶教程(四)之----Android 中自定义属性(attr.xml,TypedArray)的使用! .
- Android高手进阶教程(四)之----Android 中自定义属性(attr.xml,TypedArray)的使用!
- Android高手进阶教程(四)之----Android 中自定义属性(attr.xml,TypedArray)的使用!
- Android高手进阶教程(四)之----Android 中自定义属性(attr.xml,TypedArray)的使用!
- 第7周作业1-循环大战
- HDU 1025
- 移动开发APP开发细节分析
- svn和cvs各领风骚
- Python程序语言快速上手教程
- android 之 Attr的使用
- 只允许一个程序运行一个实例 代码
- c++如何读取文本文件中矩阵,得到矩阵的列数
- Sign Language Ring概念手语戒指 创造属于自己的手语
- 投票喜欢/不喜欢 ajax无刷新
- java 事件
- 云计算视频教程:向少数大型平台靠拢
- python 类继承与子类实例初始化
- PHP中session_set_save_handler函数的简单用法