自定义组件

来源:互联网 发布:微信红包数据协议破解 编辑:程序博客网 时间:2024/05/08 19:05

       android中,所有的UI界面都是由View和ViewGroup类及其子类组合而成。View类是所有UI组件的基类,ViewGroup类是这些UI组件的容器,其本身也是View类的子类。

一般情况下,开发Android应用程序的UI界面,使用的都是View类和ViewGroup类的子类,例如ImageView用来显示图片等。Android提供了很多继承了View类的UI组件,但是在实际开发中,还是会出现不能满足程序需要的情况,这个时候我们就需要采用自定义组件。

  自定义组件分为自定义控件widget(View)和自定义布局layout(ViewGroup)。View就是我们经常使用的如Button、ImageView、TextView等;ViewGroup就是包括但不限于LinearLayout、RelativeLayout、FrameLayout等在内的布局控件。

在Android Developers中关于自定义组件是这样介绍的(点击打开链接)。

一、自定义组件的基本步骤如下;

  1. 继承一个已有的View类或者你自己创建的View类,比如
private class SparkTextView extends TextView{}


2.重写父类中的方法--onMeasure()、onDraw()、onKeyDown();
    @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        super.onMeasure(widthMeasureSpec, heightMeasureSpec);    }    @Override    protected void onDraw(Canvas canvas) {        super.onDraw(canvas);    }    @Override    public boolean onKeyDown(int keyCode, KeyEvent event) {        return super.onKeyDown(keyCode, event);    }

3.使用自定义组件在程序XML中
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical" android:layout_width="match_parent"    android:layout_height="match_parent">    <administrator.com.knowallthing.view.SparkTextView        android:layout_width="wrap_content"        android:layout_height="wrap_content" />    </LinearLayout>

二、自定义组件详情
在上面只是简单的概括了自定义组件时的基本步骤,实际使用中自定义组件又可以分为完全自定义组件(Fully customized components)和继续已有控件(或布局)的自定义组件,还有组合式自定义组件。这些内容在我们的Android Developers中都有介绍,也有相应的sample可以参考。

2.1 完全自定义组件
优点:随心所欲的创造一切你可以想象的到的view。
方法:
①继承View类
②在xml中自定义属性和参数
③设置点击事件
④重写onMeasure()、onDraw()方法

2.1.1 继承View类
public class MyCustomComponents extends View {    public MyCustomComponents(Context context) {        super(context);    }    public MyCustomComponents(Context context, AttributeSet attrs) {        super(context, attrs);    }    public MyCustomComponents(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);    }}

2.1.2 定义属性和方法
在res/values/目录下创建attrs.xml文件
 <declare-styleable name="MyCustomComponents" >        <attr name="backgroud_color" format="color"/>        <attr name="size" format="dimension"/>            </declare-styleable>

2.1.3 设置点击事件
重写onKeyDown()、onTouchEvent()等方法即可

2.1.4 onMeasure()方法
  官方介绍:

 @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        super.onMeasure(widthMeasureSpec, heightMeasureSpec);        //调用setMeasuredDimension()方法存放视图的宽和高        setMeasuredDimension(measureWidth(widthMeasureSpec),measureHeight(heightMeasureSpec));    }    private int measureHeight(int heightMeasureSpec) {        int result = 0;        //1.使用MeasureSpec类来获得模式和大小        int specMode = MeasureSpec.getMode(heightMeasureSpec);        int heightSize = MeasureSpec.getSize(heightMeasureSpec);        //2.判断测量的模式,给出不同的值        if (specMode == MeasureSpec.EXACTLY){            //如果是精确模式,具体值或match_parent情况下            result =heightSize;        }else if (specMode == MeasureSpec.AT_MOST){            //最大模式下,即wrap_content情况下            result = 200;            result = Math.min(result,heightSize);        }else if (specMode == MeasureSpec.UNSPECIFIED){            //不指定大小        }        return result;    }
measureWidth()方法与measureHeight()基本一样。这样View的测量就完成了。在代码中出现了几个关键点:
  • setMeasureDimension(int , int);//store the measured width and height of this view
  • MeasureSpec类:使用该类来测量View,包含有测量模式和测量值。
2.1.5 onDraw()方法
先看官方介绍:

可以看到onDraw()方法传递了一个Canvas在上面,所以我们可以实现各种式样。这里牵扯到了2D graphics,不懂的可以先点击这里快速浏览下。总结起来就是想要在Canvas上画东西,需要四个基本组件--Bitmap、Canvas、drawing primitive和Paint。

2.2 继承已有控件(布局)
继承已有控件的方法比完全自定义组件更加简单,只需要重写相应的方法即可。比如,我们需要一个可以文字闪烁的TextView,可以直接继承TextView然后重写需要的方法。


2.3 组合控件(布局)
有了前面的基础,对于组合的使用也是比较简单的,我还是先贴上我们Android API中的介绍。


还是比较详细的。






0 0