Android API——自定义组件(Custom Components)

Android 提供了一组灵活和强大的组件模型来构建自己需要的UI。主要是通过继承 View 和ViewGroup。

Android提供的常用控件,其实也是继承了View 和 ViewGroup,

View的例子:

A partial list of available widgets includes ButtonTextViewEditText,ListViewCheckBoxRadioButtonGallerySpinner, and the more special-purpose AutoCompleteTextViewImageSwitcher, andTextSwitcher.

ViewGroup的例子:

Among the layouts available are LinearLayoutFrameLayoutRelativeLayout, and others. For more examples, see Common Layout Objects.


如果很不幸,这些Android为你预先准备的widgets或layouts不能满足你的需求,那么,就自己动手,丰衣足食。

如果对于现有的widgets或layouts,你只需要修改一小部分就满足需求,那么你只需要继承它,然后重写相应的方法即可;

如果需要大概,那么直接继承View或ViewGroup本身,然后重写一切。


基本方法

1、继承View

2、重写相关方法

3、使用你的View

  1. Extend an existing View class or subclass with your own class. 
  2. Override some of the methods from the superclass. The superclass methods to override start with 'on', for example, onDraw()onMeasure(), and onKeyDown(). This is similar to the on... events in Activity orListActivity that you override for lifecycle and other functionality hooks.
  3. Use your new extension class. Once completed, your new extension class can be used in place of the view upon which it was based.

你可能感兴趣的:(Android API——自定义组件(Custom Components))