Chinaunix首页 | 论坛 | 博客
  • 博客访问: 10041
  • 博文数量: 9
  • 博客积分: 251
  • 博客等级: 二等列兵
  • 技术积分: 110
  • 用 户 组: 普通用户
  • 注册时间: 2012-08-19 23:01
文章分类
文章存档

2012年(9)

我的朋友
最近访客

分类: 嵌入式

2012-08-27 08:47:35

2012年8月27日7:17:09
自定义控件一般可以基于所有控件类的基类View来实现。它可以具有公开或者保护的属性、方法、还可以具有自己的xml属性。布局文件中,自定义控件的使用方法和预定义控件的使用是类似的。
     自定义控件可以在本应用程序包中实现功能的复用,但是不能跨程序包使用。
一、View具有以下三个构造函数,分别用于在不同的场合产生View:
    public View(Context context)                                             //简单构造
    public View(Context context, AttributeSet attrs)                    //带有属性
    public View(Context context, AttributeSet attrs, int defStyle)     //带属性和默认值
二、关键方法
(1) View类中的一系列方法,用于View的运行周期内的特定时刻调用。通过实现定制实现这些方法可以定制View行为:
          protected void onFinishInflate()     //此方法将在布局XML文件Infalte完成的时刻被调用
          protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)     //用于确认View及其孩子要求的尺寸;
          protected void onLayout(boolean changed, int left, int top, int right, int bottom)     //用于在View完成时给它的孩子们分配好尺寸和位置的时刻调用;
          protected void onSizeChanged(int w, int h, int oldw, int oldh)     //用于在View的尺寸发生变化的时候调用;
             protected void onDraw(Canvas canvas)     //是View类的核心方法,在绘制的时候被调用,这是一个用于确定View外观的关键方法。
     一般情况下,在一个控件的构建过程中,依次调用onFinishInflate()、onMeasure()、onLayout()和onDraw()方法。
(2) 设备相关事件处理方法:
          public boolean onKeyDown(int keycode, KeyEvent event)     //按键按下事件
          public boolean onKeyUp(int keycode, KeyEvent event)          //按键抬起事件
          public boolean onTouchEvent(MotionEvent event)               //触摸屏事件
          public boolean onTrackballEvent(MotionEvent event)          //轨迹球事件
(3) 与聚焦相关的方法:
          public void onWindowFocusChanged(boolean hasWindowFocus)     //包含当前控件的窗口聚焦改变的情况下被调用
          protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect)//当前控件的聚焦发生变化的时候被调用;

实例自定义TextView
testView.java

点击(此处)折叠或打开

  1. public class testView extends View
  2. {
  3.     Paint mPaint;
  4.     public testView(Context context)
  5.     {
  6.         super(context);
  7.     }
  8.     
  9.     public testView(Context context, AttributeSet attrs)
  10.     {
  11.         super(context, attrs);
  12.         mPaint = new Paint();
  13.       TypedArray array = context.obtainStyledAttributes(attrs,R.styleable.testView);
  14.         //提供默认值放置位置未定
  15.         int textColor = array.getColor(R.styleable.testView_textColor, 0xFF00ff00);
  16.         float textSize = array.getDimension(R.styleable.testView_textSize, 36);
  17.         mPaint.setColor(textColor);
  18.         mPaint.setTextSize(textSize);
  19.         array.recycle();
  20.     }
  21.     
  22.     public void onDraw(Canvas canvas)
  23.     {
  24.         super.onDraw(canvas);
  25.         mPaint.setStyle(Style.FILL);
  26.         canvas.drawRect(10, 10, 100, 100, mPaint);
  27.         mPaint.setColor(Color.BLUE);
  28.         canvas.drawText("自定义View测试", 10, 120, mPaint);
  29.     }
  30. }
styleable.xml

点击(此处)折叠或打开

  1. <resources>
  2.     <declare-styleable name="testView">
  3.         <attr name="textColor" format="color"/>
  4.         <attr name="textSize" format="dimension"/>
  5.     </declare-styleable>
  6. </resources>
布局文件:

点击(此处)折叠或打开

  1. <RelativeLayout xmlns:android=""
  2.     xmlns:tools=""
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent" xmlns:app="">
  5.     <com.example.test002.testView
  6.         android:layout_width="fill_parent"
  7.         android:layout_height="wrap_content"
  8.         ></com.example.test002.testView>
  9. </RelativeLayout>
源码: test002.zip   
这份代码有点小问题,等再修改吧上班了要~
           
阅读(624) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~