书接上回

在xml里建立属性,然后java代码里用typedArray获得这些属性,得到属性后,利用属性做一些事.例:得到xml里的color,赋给paint.

1.在res/values/下新建attrs.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <declare-styleable name="CustomView2">
  4. <attr name="textColor" format="color" />
  5. <attr name="textSize" format="dimension" />
  6. </declare-styleable>
  7. </resources>
  8. <!-- name="CustomView1"控件名称 得到TypedArray时用 -->
  9. <!-- name="textColor" 对应test:textColor -->
  10. <!-- format="color" 对应构造方法里a.getColor(R.styleable.CustomView2_textColor, 0xFFFFFFFF); -->

format详解可参照http://blog.csdn.net/ethan_xue/article/details/7315064
2.主要看构造函数

  1. public class CustomView2 extends View {
  2. private Paint mPaint2;
  3. private String mText = "drawText";
  4. public CustomView2(Context context, AttributeSet attrs) {
  5. super(context, attrs);
  6. mPaint2 = new Paint();
  7. // TypedArray是存放资源的array,1.通过上下文得到这个数组,attrs是构造函数传进来的,对应attrs.xml
  8. TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomView2);
  9. // 获得xml里定义的属性,格式为 名称_属性名 后面是默认值
  10. int textColor = a.getColor(R.styleable.CustomView2_textColor, 0xFFFFFFFF);
  11. float textSize = a.getDimension(R.styleable.CustomView2_textSize, 35);
  12. mPaint2.setColor(textColor);
  13. mPaint2.setTextSize(textSize);
  14. // 为了保持以后使用该属性一致性,返回一个绑定资源结束的信号给资源
  15. a.recycle();
  16. }
  17. @Override
  18. protected void onDraw(Canvas canvas) {
  19. super.onDraw(canvas);
  20. mPaint2.setStyle(Style.FILL);
  21. canvas.drawText(mText, 10, 60, mPaint2);
  22. }
  23. }

3.布局

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!-- xmlns:test="http://schemas.android.com/apk/res/ethan.customview1" 包名 -->
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  4. xmlns:test="http://schemas.android.com/apk/res/ethan.customview1"
  5. android:orientation="vertical"
  6. android:layout_width="fill_parent"
  7. android:layout_height="fill_parent"
  8. >
  9. <ethan.customview1.CustomView2
  10. android:layout_width="wrap_content"
  11. android:layout_height="wrap_content"
  12. test:textColor="#f00"
  13. test:textSize="20sp"
  14. />
  15. </LinearLayout>

4.效果图

下载地址 http://download.csdn.net/detail/ethan_xue/4108832

最新文章

  1. vmware 虚拟机中添加新网卡无配置文件
  2. 通过“回文字算法”复习C++语言。
  3. 0018 Java学习笔记-面向对象-类的基本要素
  4. 使用ASP.NET Web Api构建基于REST风格的服务实战系列教程【五】——在Web Api中实现Http方法(Put,Post,Delete)
  5. C语言语句分类:大致可分为六大类
  6. Debian下配置网络的方法
  7. ios开发——面试篇(一)
  8. AngularJs的UI组件ui-Bootstrap-Tooltip
  9. WinMain与WndProc以及窗口诞生过程总结
  10. 基于clip-path的任意元素的碎片拼接动效(源自鑫空间)
  11. Linux学习(十四)磁盘格式化、磁盘挂载、手动增加swap空间
  12. Ubuntu16.04部署phantomjs的一个问题
  13. 黄文俊:Serverless小程序后端技术分享
  14. Python bytes数据类型
  15. One-hot encoding 独热编码
  16. jQuery-jqprint.js打印插件使用高版本jQuery时问题
  17. PHP语句函数
  18. &lt;转&gt;jmeter(二)录制脚本
  19. 转:display:flex不兼容Android、Safari低版本的解决方案 【flex布局】
  20. 针对Windows 64位系统中Matlab没有LED Control Activex控件的解决方法

热门文章

  1. Xen on Ubuntu
  2. Redis主从同步分析
  3. Android后退事件的处理
  4. client交互技术简单介绍
  5. Hadoop起源
  6. Nginx负载均衡简易配置
  7. 15 redis 之 aof恢复与rdb服务器间迁移
  8. View数据呈现相关技术
  9. cmake使用第三方库
  10. java参数的值传递和引用传递