View
public class ViewextendsObjectimplementsDrawable.Callback,KeyEvent.Callback,AccessibilityEventSource
-> android.view.View
直接子类 AnalogClock,ImageView,KeyboardView,MediaRouteButton,ProgressBar,Space,SurfaceView,TabItem,TextView,TextureView,ViewGroup,ViewStub |
|---|
间接子类 AbsListView,AbsSeekBar,AbsSpinner,AbsoluteLayout,ActionMenuView,AdapterView<T extends Adapter>,AdapterViewAnimator,AdapterViewFlipper,and119 others. |
|---|
此类表示用户界面组件的基本构建块. 一个View表示占据屏幕上的矩形区域,并负责绘图和事件处理. View是小部件的基类, 小部件用来创建可交互UI组件 (buttons, text fields, etc.). ViewGroup子类是布局的基类, 它是包含Views (或者其它的ViewGroups)的对用户不可见的容器, 它可以用来定义它们属性.
开发者指导
关于使用这个类来开发应用的界面的信息, 阅读User Interface开发者指导.
使用View
窗口中的所有视图都安排在单颗树中. 可以通过代码或者在一个或多个XML布局文件中加入指定View的树型结构. There are many specialized subclasses of views that act as controls or are capable of displaying text, images, or other content.
Once you have created a tree of views, there are typically a few types of common operations you may wish to perform:
- Set properties:
for example setting the text of a
TextView. The available properties and the methods that set them will vary among the different subclasses of views. Note that properties that are known at build time can be set in the XML layout files. - Set focus:
The framework will handle moving focus in response to user input. To force focus to a specific view, call
requestFocus(). - Set up listeners:
Views allow clients to set listeners that will be notified when something interesting happens to the view. For example, all views will let you set a listener to be notified when the view gains or loses focus. You can register such a listener using
setOnFocusChangeListener(android.view.View.OnFocusChangeListener). Other view subclasses offer more specialized listeners. For example, a Button exposes a listener to notify clients when the button is clicked. - Set visibility:
You can hide or show views using
setVisibility(int).
Note: The Android framework is responsible for measuring, laying out and drawing views. You should not call methods that perform these actions on views yourself unless you are actually implementing aViewGroup.
Implementing a Custom View
To implement a custom view, you will usually begin by providing overrides for some of the standard methods that the framework calls on all views. You do not need to override all of these methods. In fact, you can start by just overridingonDraw(android.graphics.Canvas).
| 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. |
onFinishInflate() |
Called after a view and all of its children has been inflated from XML. | |
| Layout | onMeasure(int, int) |
Called to determine the size requirements for this view and all of its children. |
onLayout(boolean, int, int, int, int) |
Called when this view should assign a size and position to all of its children. | |
onSizeChanged(int, int, int, int) |
Called when the size of this view has changed. | |
| Drawing | onDraw(android.graphics.Canvas) |
Called when the view should render its content. |
| Event processing | onKeyDown(int, KeyEvent) |
Called when a new hardware key event occurs. |
onKeyUp(int, KeyEvent) |
Called when a hardware key up event occurs. | |
onTrackballEvent(MotionEvent) |
Called when a trackball motion event occurs. | |
onTouchEvent(MotionEvent) |
Called when a touch screen motion event occurs. | |
| Focus | onFocusChanged(boolean, int, android.graphics.Rect) |
Called when the view gains or loses focus. |
onWindowFocusChanged(boolean) |
Called when the window containing the view gains or loses focus. | |
| Attaching | onAttachedToWindow() |
Called when the view is attached to a window. |
onDetachedFromWindow() |
Called when the view is detached from its window. | |
onWindowVisibilityChanged(int) |
Called when the visibility of the window containing the view has changed. |
IDs
Views may have an integer id associated with them. These ids are typically assigned in the layout XML files, and are used to find specific views within the view tree. A common pattern is to:
- Define a Button in the layout file and assign it a unique ID. ``` < Button
android:id
=
"@+id/my_button"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:text
=
"@string/my_button_text"
/
>
* From the onCreate method of an Activity, find the Button
Button
myButton
=
findViewById
(
R
.
id
.
my_button
);
```
View IDs need not be unique throughout the tree, but it is good practice to ensure that they are at least unique within the part of the tree you are searching.
