1. 程式人生 > 實用技巧 >android view原始碼

android view原始碼

1.view標頭檔案的翻譯

This class represents the basic building block for user interface components.
當前類是使用者頁面元件的基本構建模組。(view是頁面的基本)

A View occupies a rectangular area on the screen and is responsible for drawing and event handling.
檢視佔有螢幕矩形區域並且負責繪製和事件處理(view的功能:在螢幕矩形區域繪製檢視以及檢視的事件處理)

View is the base class for <em>widgets</em>, which are used to create interactive UI components (buttons, text fields, etc.).

View(widgets的基類)是被用來建立互動式UI的元件

The {@link android.view.ViewGroup} subclass is the base class for <em>layouts</em>, which are invisible containers that hold other Views (or other ViewGroups) and define their layout properties.
ViewGroup(view的子類,layout的基類)是不可見的容器並且包含其它的views/viewgroups,它定義了layout的屬性。


Developer Guides
For information about using this class to develop your application's user interface,
read the <a href="{@docRoot}guide/topics/ui/index.html">User Interface</a> developer guide.
國內連結地址:https://developer.android.google.cn/guide/topics/ui/index.html

U
seing
All of the views in a window are arranged in a single tree.

視窗中的所有檢視都排列在一棵樹中

You can add views either from code or by specifying a tree of views in one or more XML layout files.
可以通過程式碼或通過在一個或多個XML佈局中指定檢視樹來新增檢視

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:
建立檢視樹後,通常可能需要執行幾種型別的常見操作:比如為textview設定text

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
請注意,可以在XML佈局檔案中設定*在構建時已知的屬性*
set focus

The framework will handle moving focus in response to user input. To force focus to a specific view, call {@link #requestFocus}
該框架將響應使用者輸入來處理移動焦點。 想強制聚焦到特定檢視,參考* {@link #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 {@link #setOnFocusChangeListener(android.view.View.OnFocusChangeListener)}.
焦點的監聽是setOnFoucusChangeListener

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 {@link #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 a {@link android.view.ViewGroup}.
注意:Android framework負責測量,佈局和繪圖檢視。 除非實際實現的是{{link android.view.ViewGroup},否則不應自己對檢視回撥這些操作的方法
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 overriding {@link #onDraw(android.graphics.Canvas)}
不需要重寫所有這些方法。 實際上,可以只重寫{@link #onDraw(android.graphics.Canvas)}
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 {@link #onFinishInflate()}

Called to determine the size requirements for this view and all of its children. {@link #onMeasure(int, int)}
Called when this view should assign a size and position to all of its children. {@link #onLayout(boolean, int, int, int, int)}

Called when the size of this view has changed.
{@link #onSizeChanged(int, int, int, int)}

Called when the view should render its content.
{@link #onDraw(android.graphics.Canvas)}

Called when a new hardware key event occurs. {@link #onKeyDown(int, KeyEvent)}

Called when a hardware key up event occurs.
{@link #onKeyUp(int, KeyEvent)}
Called when a trackball motion event occurs. {@link #onTrackballEvent(MotionEvent)}
Called when a touch screen motion event occurs. {@link #onTouchEvent(MotionEvent)}
Called when the view gains or loses focus. {@link #onFocusChanged(boolean, int, android.graphics.Rect)}
Called when the window containing the view gains or loses focus. {@link #onWindowFocusChanged(boolean)}
Called when the view is attached to a window. {@link #onAttachedToWindow()}
Called when the view is detached from its window. {@link #onDetachedFromWindow}

Called when the visibility of the window containing the view has changed. {@link #onWindowVisibilityChanged(int)}