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文件:

[html]  view plain copy
  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>  
         首先,声明自定义,nameFlowIndicator,属性设置为比较简单的格式,前面参数name,后面是参数格式。

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

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

然后这样使用:

[java]  view plain copy
  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.   
  11.         point_normal_color = typeArray.getColor(  
  12.                 R.styleable.FlowIndicator_point_normal_color, 0x000000);  
  13.         point_seleted_color = typeArray.getColor(  
  14.                 R.styleable.FlowIndicator_point_seleted_color, 0xffff07);  
  15.   
  16.         int sum = attrs.getAttributeCount();  
  17.         if (Constans.DEBUG) {  
  18.             String str = "";  
  19.             for (int i = 0; i < sum; i++) {  
  20.                 String name = attrs.getAttributeName(i);  
  21.                 String value = attrs.getAttributeValue(i);  
  22.                 str += "attr_name :" + name + ": " + value + "\n";  
  23.             }  
  24.             Log.i("attribute", str);  
  25.         }  
  26.         typeArray.recycle();  
  27.     }  

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

[java]  view plain copy
  1. Give back a previously retrieved StyledAttributes, for later re-use.  

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

在获取属性值的时候所用类似R.styleable.FlowIndicator_count的方式,其中FlowIndicator_count是采取的名字_属性这种格式。定义好了自定义属性,就可以在自定控件中的属性设置了:

[html]  view plain copy
  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:这样设置自定义的属性了。

如果控件继承自某个View类,那么在定义属性的时候就不用考虑布局的属性的了;

更多相关文章

  1. mybatisplus的坑 insert标签insert into select无参数问题的解决
  2. Python技巧匿名函数、回调函数和高阶函数
  3. python list.sort()根据多个关键字排序的方法实现
  4. android 图片全屏
  5. [置顶] android AIDL 进程间通信
  6. 安卓shape属性详细介绍
  7. 高德地图Android,绘制自定义定位蓝点、marker、面
  8. 关于Android调用系统Action录制视频
  9. android xml属性的单位大全

随机推荐

  1. android findviewbyId 简化写法
  2. android JNI 多线程 C函数回调
  3. Android(安卓)LiveData 使用
  4. Android(安卓)Studio的Beta, Canary, Dev
  5. Ubuntu12.04 adb无法连接手机
  6. Android创建旋转箭头-不恢复原位-旋转动
  7. 基于CSipSimple开发的VOIP网络电话 iCall
  8. Android--H5交互简介
  9. Android(安卓)Dialog 生命周期
  10. android 混淆配置