Android(安卓)API——自定义组件(Custom Components)
16lz
2022-03-08
Android 提供了一组灵活和强大的组件模型来构建自己需要的UI。主要是通过继承 View 和ViewGroup。
Android提供的常用控件,其实也是继承了View 和 ViewGroup,
View的例子:
A partial list of available widgets includes Button
, TextView
, EditText
,ListView
, CheckBox
, RadioButton
, Gallery
, Spinner
, and the more special-purpose AutoCompleteTextView
, ImageSwitcher
, andTextSwitcher
.
ViewGroup的例子:
Among the layouts available are LinearLayout
, FrameLayout
, RelativeLayout
, and others. For more examples, see Common Layout Objects.
如果很不幸,这些Android为你预先准备的widgets或layouts不能满足你的需求,那么,就自己动手,丰衣足食。
如果对于现有的widgets或layouts,你只需要修改一小部分就满足需求,那么你只需要继承它,然后重写相应的方法即可;
如果需要大概,那么直接继承View或ViewGroup本身,然后重写一切。
基本方法
1、继承View
2、重写相关方法
3、使用你的View
- Extend an existing
View
class or subclass with your own class. - Override some of the methods from the superclass. The superclass methods to override start with '
on
', for example,onDraw()
,onMeasure()
, andonKeyDown()
. This is similar to theon...
events inActivity
orListActivity
that you override for lifecycle and other functionality hooks. - 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之获取加载图片宽高问题(getIntrinsicWidth与getwidth的区
- Android模拟用户点击的实现方法
- 在使用ListFragment的setEmptyText时报java.lang.IllegalStateEx
- 查看基于Android(安卓)系统单个进程内存、CPU使用情况的几种方法
- WebKit – WebKit For Android
- Android(安卓)Http请求方法汇总
- android开关飞行模式的方法
- 浅谈Java中Collections.sort对List排序的两种方法
- Python list sort方法的具体使用