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.

更多相关文章

  1. 详解Android获取系统内核版本的方法与实现代码
  2. Android模拟用户点击的实现方法
  3. [置顶] Android下实现自动关机的方法总结
  4. Mac完整卸载Android Studio的方法
  5. 【Android】Android处理Home键方法小结
  6. Android常用方法——获取系统信息

随机推荐

  1. android NDK学习篇3之two-libs——使用(多
  2. Android属性之excludeFromRecents
  3. 一些关于RelativeLayout属性的用法
  4. 【Android(安卓)开源系列】之视频处理框
  5. Android(安卓)支持多屏幕机制
  6. android事件分发机制
  7. Android(安卓)Handler
  8. Android(安卓)SDK 1-23各版本代号和别名
  9. singleLine=”true” 和 maxLines=”1″
  10. Android(安卓)开发中遇到的 bug(6)