android自定义视图属性(atts.xml,TypedArray)学习
是一个用于存放恢复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>
首先,声明自定义<declare-styleable name="FlowIndicator">,nameFlowIndicator,属性设置为比较简单的格式,前面参数name,后面是参数格式。
自定义属性的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>
首先,要有声明:
xmlns:app="http://schemas.android.com/apk/res/com.dream.myqiyi",“com.dream.myqiyi”这个是你项目的包名。
然后我们就可以使用app:这样设置自定义的属性了。
- app:point_normal_color="#45000000"
- app:point_radius="3dip"
- app:point_seleted_color="#ffffff"
- app:point_size="5dip"
- app:space="10dip"
android自定义视图属性(atts.xml,TypedArray)学习的更多相关文章
- Android自定义视图一:扩展现有的视图,添加新的XML属性
这个系列是老外写的,干货!翻译出来一起学习.如有不妥,不吝赐教! Android自定义视图一:扩展现有的视图,添加新的XML属性 Android自定义视图二:如何绘制内容 Android自定义视图三: ...
- Android自定义视图教程
Android自定义视图教程 Android的UI元素都是基于View(屏幕中单个元素)和ViewGroup(元素的集合),Android有许多自带的组件和布局,比如Button.TextView.R ...
- Android自定义视图四:定制onMeasure强制显示为方形
这个系列是老外写的,干货!翻译出来一起学习.如有不妥,不吝赐教! Android自定义视图一:扩展现有的视图,添加新的XML属性 Android自定义视图二:如何绘制内容 Android自定义视图三: ...
- Android自定义视图三:给自定义视图添加“流畅”的动画
这个系列是老外写的,干货!翻译出来一起学习.如有不妥,不吝赐教! Android自定义视图一:扩展现有的视图,添加新的XML属性 Android自定义视图二:如何绘制内容 Android自定义视图三: ...
- Android自定义视图二:如何绘制内容
这个系列是老外写的,干货!翻译出来一起学习.如有不妥,不吝赐教! Android自定义视图一:扩展现有的视图,添加新的XML属性 Android自定义视图二:如何绘制内容 Android自定义视图三: ...
- 【转】ANDROID自定义视图——onLayout源码 流程 思路详解
转载(http://blog.csdn.net/a396901990) 简介: 在自定义view的时候,其实很简单,只需要知道3步骤: 1.测量——onMeasure():决定View的大小 2.布局 ...
- 【转】ANDROID自定义视图——onMeasure,MeasureSpec源码 流程 思路详解
原文地址:http://blog.csdn.net/a396901990/article/details/36475213 简介: 在自定义view的时候,其实很简单,只需要知道3步骤: 1.测量—— ...
- ANDROID自定义视图——onMeasure,MeasureSpec源码 流程 思路详解
简介: 在自定义view的时候,其实很简单,只需要知道3步骤: 1.测量--onMeasure():决定View的大小 2.布局--onLayout():决定View在ViewGroup中的位置 3. ...
- Android开发UI之自定义视图属性
Android框架中,所有自定义的view类都继承自View,也可以继承Button等view的子类 为了允许ADT能够与view交互,必须提供一个能够获取Context和作为属性的Attribute ...
随机推荐
- Buffer 和Cache 的区别
Buffer 和Cache 的区别buffer 与cache 操作的对象就不一样.buffer
- ACM学习历程—SNNUOJ1213 加油站问题(动态规划 || 数学)
题目链接:http://219.244.176.199/JudgeOnline/problem.php?id=1213 这是这次微软实习面试的一道题,当时只相出了一个2n的做法,面试官让我优化成n的做 ...
- Oracl使用总结二
1.ORA-00972: 标识符过长 错误排除 可能原因: 1.如果是拼接成的sql语句,请查找传递参数时字符型字段是否两边少了引号.2.数据库表名太长了,附各种类型的数据库表名长度: SQLSERV ...
- unix下网络编程之I/O复用(二)
select函数 该函数允许进程指示内核等待多个事件中的任何一个发生,并仅在有一个或是多个事件发生或经历一段指定的时间后才唤醒它.我们调用select告知内核对哪些描述字(就读.写或异常条件)感兴趣以 ...
- PCIE编程1:lspci操作
lspci 是一个用来显示系统中所有PCI总线设备或连接到该总线上的所有设备的工具. 列出所有的PCIE设备: lspci 选项: -v 使得 lspci 以冗余模式显示所有设备的详细信息. -vv ...
- Python学习笔记 - MySql的使用
一.安装MySql模块 Python2.X pip install MySQLdb Python3.X pip install pymysql 二.数据库连接接口 由于Python统一了数据库连接的接 ...
- HDU2579(bfs迷宫)
Dating with girls(2) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- 为什么 Eclipse 里的 Classpath Variables M2_REPO 无法修改(non modifiable)
本文转载自:http://uule.iteye.com/blog/2034097 解决方法: 在C:\Documents and Settings\Administrator\.m2中放入settin ...
- HashSet中是如何判断元素是否重复的
HashSet不能添加重复的元素,当调用add(Object)方法时候, 首先会调用Object的hashCode方法判hashCode是否已经存在,如不存在则直接插入元素: 如果已存在则调用Obje ...
- Python多进程-进程间数据的传递
两个进程间的数据是独立的,要进行数据传递的话可通过几个方法 Queue 通过队列来进行进程间数据的传递 # -*- coding:utf-8 -*- __author__ = "MuT6 S ...