基于两个基础的类:View和ViewGroup,Android提供了一个精致而强大的组件化的用户界面。此前,Android平台预先提供了各种各样的View和ViewGroup的子类——被称作widgets和layouts。当然你也可以单独的构建你自己的ui。
可用的widgets包括 Button
, TextView
, EditText
, ListView
,CheckBox
, RadioButton
, Gallery
, Spinner
,还有基于特殊用途的AutoCompleteTextView
, ImageSwitcher
, and TextSwitcher
.
可用的layouts有 LinearLayout
, FrameLayout
, RelativeLayout
, 等,更多例子请查看 Common Layout Objects.
如果系统预先提供的widgets和layouts不能满足你的需求,你可以创建你自己的View子类。如果你想对系统提供的widgets和layouts做修改,你可以继承它们并重载它们的方法。
如果创建自定义的View的子类,你可以精确的控制它的外观和功能。下面是你能对自定义控件做的控制:
1 你可以创建一个完全自定义渲染方式的控件,例如一个用2d图形渲染的把手,类似于模拟电子控制。
2你可以将多个view组合在一起,成为一个单独的组件。
3你可以重载EditText在屏幕上的渲染方式(Notepad Tutorial 通过这个方式来达到好的效果)
4你可以捕捉其他的事件,并用自定义的方式处理他们。
下面的部分解释如何创造自定义控件,并在app中使用他们:
onDraw()
,
onMeasure()
, and
onKeyDown()
.这和 Activity
or ListActivity
中的你复写on开头的方法一样。
onDraw()
and onMeasure()
Canvas类 ,通过它你可以实现任何图形:2d图形,其他的标准或自定义的控件,文字字体等
Category | Methods | Description |
---|---|---|
Creation | Constructors | There is a form of the constructor that are called when the view is created from code and a form that is called when the view is inflated from a layout file. The second form should parse and apply any attributes defined in the layout file. |
|
Called after a view and all of its children has been inflated from XML. | |
Layout |
|
Called to determine the size requirements for this view and all of its children. |
|
Called when this view should assign a size and position to all of its children. | |
|
Called when the size of this view has changed. | |
Drawing |
|
Called when the view should render its content. |
Event processing |
|
Called when a new key event occurs. |
|
Called when a key up event occurs. | |
|
Called when a trackball motion event occurs. | |
|
Called when a touch screen motion event occurs. | |
Focus |
|
Called when the view gains or loses focus. |
|
Called when the window containing the view gains or loses focus. | |
Attaching |
|
Called when the view is attached to a window. |
|
Called when the view is detached from its window. | |
|
Called when the visibility of the window containing the view has changed. |