【eoe 3】自定义控件

一、继承控件类、组合方式、绘制控件

  1.View的工作原理

  2.编写View类

  3.为View类增加属性

  4.绘制屏幕

  5.响应用户消息  

  6.自定义回调函数

二、

  主要是实现重载onDraw函数,通过Canvas类来重绘视图

三、

  编写attrs.xml文件

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

  format对应自定义变量 的类型

四、

  在构造函数里面,通过

TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.IconTextView);// TypedArray是存放资源的array,1.通过上下文得到这个数组,attrs是构造函数传进来的,对应attrs.xml         
resourceId = typedArray.getResourceId(R.styleable.IconTextView_iconSrc,0); // 获得xml里定义的属性,格式为 名称_属性名 后面是默认值  



获取控件各个属性。

posted @ 2013-07-13 16:28  谈笑风生膜法师  阅读(238)  评论(0编辑  收藏  举报