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

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

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <declare-styleable name="FlowIndicator">
  4. <attr name="count" format="integer" />
  5. <attr name="space" format="dimension" />
  6. <attr name="point_size" format="dimension" />
  7. <attr name="point_seleted_color" format="color|reference" />
  8. <attr name="point_normal_color" format="color|reference" />
  9. <attr name="point_radius" format="dimension" />
  10. </declare-styleable>
  11. </resources>

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

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

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

然后这样使用:

  1. public FlowIndicator(Context context, AttributeSet attrs) {
  2. super(context, attrs);
  3. //获得实例
  4. TypedArray typeArray = context.obtainStyledAttributes(attrs,
  5. R.styleable.FlowIndicator);
  6. //从typeArray获取相应值,第二个参数为默认值,如第一个参数在atts.xml中没有定义,返回第二个参数值
  7. count = typeArray.getInteger(R.styleable.FlowIndicator_count, 4);
  8. space = typeArray.getDimension(R.styleable.FlowIndicator_space, 9);
  9. radius = typeArray.getDimension(R.styleable.FlowIndicator_point_radius, 9);
  10. point_normal_color = typeArray.getColor(
  11. R.styleable.FlowIndicator_point_normal_color, 0x000000);
  12. point_seleted_color = typeArray.getColor(
  13. R.styleable.FlowIndicator_point_seleted_color, 0xffff07);
  14. int sum = attrs.getAttributeCount();
  15. if (Constans.DEBUG) {
  16. String str = "";
  17. for (int i = 0; i < sum; i++) {
  18. String name = attrs.getAttributeName(i);
  19. String value = attrs.getAttributeValue(i);
  20. str += "attr_name :" + name + ": " + value + "\n";
  21. }
  22. Log.i("attribute", str);
  23. }
  24. typeArray.recycle();
  25. }

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

  1. Give back a previously retrieved StyledAttributes, for later re-use.

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

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

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

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res/com.dream.myqiyi"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. android:orientation="vertical" >
  7. <com.dream.myqiyi.widget.FlowIndicator
  8. android:id="@+id/myView"
  9. android:layout_width="fill_parent"
  10. android:layout_height="wrap_content"
  11. android:layout_marginBottom="5dip"
  12. app:count="4"
  13. android:gravity="center"
  14. app:point_normal_color="#45000000"
  15. app:point_radius="3dip"
  16. app:point_seleted_color="#ffffff"
  17. app:point_size="5dip"
  18. app:space="10dip" />
  19. </FrameLayout>

首先,要有声明:

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

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

    1. app:point_normal_color="#45000000"
    2. app:point_radius="3dip"
    3. app:point_seleted_color="#ffffff"
    4. app:point_size="5dip"
    5. app:space="10dip"

android自定义视图属性(atts.xml,TypedArray)学习的更多相关文章

  1. Android自定义视图一:扩展现有的视图,添加新的XML属性

    这个系列是老外写的,干货!翻译出来一起学习.如有不妥,不吝赐教! Android自定义视图一:扩展现有的视图,添加新的XML属性 Android自定义视图二:如何绘制内容 Android自定义视图三: ...

  2. Android自定义视图教程

    Android自定义视图教程 Android的UI元素都是基于View(屏幕中单个元素)和ViewGroup(元素的集合),Android有许多自带的组件和布局,比如Button.TextView.R ...

  3. Android自定义视图四:定制onMeasure强制显示为方形

    这个系列是老外写的,干货!翻译出来一起学习.如有不妥,不吝赐教! Android自定义视图一:扩展现有的视图,添加新的XML属性 Android自定义视图二:如何绘制内容 Android自定义视图三: ...

  4. Android自定义视图三:给自定义视图添加“流畅”的动画

    这个系列是老外写的,干货!翻译出来一起学习.如有不妥,不吝赐教! Android自定义视图一:扩展现有的视图,添加新的XML属性 Android自定义视图二:如何绘制内容 Android自定义视图三: ...

  5. Android自定义视图二:如何绘制内容

    这个系列是老外写的,干货!翻译出来一起学习.如有不妥,不吝赐教! Android自定义视图一:扩展现有的视图,添加新的XML属性 Android自定义视图二:如何绘制内容 Android自定义视图三: ...

  6. 【转】ANDROID自定义视图——onLayout源码 流程 思路详解

    转载(http://blog.csdn.net/a396901990) 简介: 在自定义view的时候,其实很简单,只需要知道3步骤: 1.测量——onMeasure():决定View的大小 2.布局 ...

  7. 【转】ANDROID自定义视图——onMeasure,MeasureSpec源码 流程 思路详解

    原文地址:http://blog.csdn.net/a396901990/article/details/36475213 简介: 在自定义view的时候,其实很简单,只需要知道3步骤: 1.测量—— ...

  8. ANDROID自定义视图——onMeasure,MeasureSpec源码 流程 思路详解

    简介: 在自定义view的时候,其实很简单,只需要知道3步骤: 1.测量--onMeasure():决定View的大小 2.布局--onLayout():决定View在ViewGroup中的位置 3. ...

  9. Android开发UI之自定义视图属性

    Android框架中,所有自定义的view类都继承自View,也可以继承Button等view的子类 为了允许ADT能够与view交互,必须提供一个能够获取Context和作为属性的Attribute ...

随机推荐

  1. WordPress 中文图片 上传 自动重命名

    由于国人很少有在上传图片前将图片名重命名为英语的,所以自动重命名对于WP来说尤为重要,特别是LINUX的不支持中文名的. WordPress上传多媒体的代码都存放于\wp-admin\includes ...

  2. 重温CLR(一)CLR基础

    如果一个C#developer,对CLR没有了解,那就只能是入门级别.未来.NET CORE是趋势,但是.NET CORE 也是基于CoreCLR的,而CLR和CoreCLR其实差别不大,从runti ...

  3. RabbitMQ 权限分离&HA操作文档

    概要 默认情况下,使用帐号guest帐号登陆MQ,所有用户的queue信息,全部创建在根目录/的virtual host下,而这样,就会导致,任一用户登录后,都能看到其他用户的queue信息. 针对以 ...

  4. Mybatis相关SQL操作总结

    1.resultMap和resultType等参数以及结果集 <select id="getApplicationByRoleCode" resultType="p ...

  5. 推荐几本学习MySQL的好书

    转载:http://mingxinglai.com/cn/2015/12/material-of-mysql/ 我这里推荐几本MySQL的好书,应该能够有效避免学习MySQL的弯路,并且达到一个不错的 ...

  6. 释放Windows C盘空间 -- 虚拟内存和休眠文件

    本文由Suzzz原创,发布于 http://www.cnblogs.com/Suzzz/p/4111718.html ,转载请保留此声明. 项目组有一Windows工作站, 由于需要使用Kinect最 ...

  7. [转]200 OK (from cache) 与 304 Not Modified------没有这个规则(ETag是否移除)!!!from cache和304,请查看顶部的流程图!

    //========没有这个规则(ETag是否移除) 20160422============// 200 OK (from cache) 与 304 Not Modified 为什么有的缓存是 20 ...

  8. bzoj 3505 [Cqoi2014]数三角形——排列组合

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3505 好题!一定要经常回顾! 那个 一条斜线上的点的个数是其两端点横坐标之差和纵坐标之差的g ...

  9. bae3.0第一步 添加框架支持

    1.克隆bae上应用代码: 先在本地linux机器上创建文件夹bae并进入, 再执行git clone https://git.duapp.com/appidd01iud80bg 结果会在bae文件夹 ...

  10. dirs命令

    dirs命令显示当前目录栈中的所有记录(不带参数的dirs命令显示当前目录栈中的记录).dirs始终显示当然目录, 再是堆栈中的内容:即使目录堆栈为空, dirs命令仍然只显示当然目录. 语法 dir ...