* This class represents the basic building block for user interface components. A View * occupies a rectangular area on the screen and is responsible for drawing and * event handling. View is the base class for widgets, which are * used to create interactive UI components (buttons, text fields, etc.). The * {@link android.view.ViewGroup} subclass is the base class for layouts, which * are invisible containers that hold other Views (or other ViewGroups) and define * their layout properties. *
這是view類開始的介紹,我幫大家翻譯一下,這段英文時期已經很言簡意賅的介紹了View類。 首現說一下,我的英文就是個菜雞,所以我就用百度翻譯給大家介紹一下。注意一下:括號外面的是百度翻譯,括號裏面的是我的翻譯,如果一句話沒有帶括號就說明我的意思跟百度翻譯的是一樣的。 <p> 這個類表示用戶界面組件的基本構建塊(表示所有的界面組件都是view)。視圖在屏幕上佔據一個矩形區域,負責繪圖和事件處理。View是用於創建交互式UI組件(按鈕、文本字段等)的“組件”的基類。這個{@鏈接Android。觀。ViewGroup }類是< EM >佈局< / EM >的基類,它是看不見的容器持有其他觀點(或其他viewgroup)並確定其佈局屬性。(ViewGroup 是所有佈局的基類),它是看不見的容器持有View或者ViewGroup 並定義它們的佈局屬性。
這就是google 大神對View類的說明。
* <div class="special reference">
* <h3>Developer Guides</h3>
* <p>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.
* </div>
有關使用此類開發應用程序用戶界面的信息 可以參考guide/topics/ui/index.html這個開發文檔。 大家在查東西的時候可以看到這個文檔,如果裏面的英文看的吃力的話,可以選擇中文。文檔地址見下圖。![ 這個是上面文檔的地址](https://img-blog.csdn.net/20170306131245551?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXFfMjg4NjU5ODk=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)。
* <a name="Using"></a>
* <h3>Using Views</h3>
* <p>
* 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. There are many specialized subclasses of views that act as controls or
* are capable of displaying text, images, or other content.
* </p>
窗口中的所有視圖都被安排在一棵樹中。您可以添加視圖從代碼或通過在一個或多個xml佈局中指定視圖樹文件夾.有許多專門的子視圖作爲控件或能夠顯示文本、圖像或其他內容。(這裏大概介紹一下怎樣使用View和View的樹結構)
* <p>
* Once you have created a tree of views, there are typically a few types of
* common operations you may wish to perform:
* <ul>
* <li><strong>Set properties:</strong> for example setting the text of a
* {@link android.widget.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.</li>
* <li><strong>Set focus:</strong> The framework will handled moving focus in
* response to user input. To force focus to a specific view, call
* {@link #requestFocus}.</li>
* <li><strong>Set up listeners:</strong> 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)}.
* Other view subclasses offer more specialized listeners. For example, a Button
* exposes a listener to notify clients when the button is clicked.</li>
* <li><strong>Set visibility:</strong> You can hide or show views using
* {@link #setVisibility(int)}.</li>
* </ul>
* </p>
一旦你創建了一個視圖樹,通常有幾種類型的常見的操作,你可能希望執行:
1.設置屬性:例如設置{@鏈接Android TextView }。小部件。可用的性質和方法它們在不同的子視圖中會有所不同。請注意,在生成時已知的屬性可以在xml佈局中設置文件夾.(根據子View的屬性設置屬性)
2.設置焦點將處理用戶輸入響應。強制聚焦到特定視圖,調用{“鏈接# requestfocus }(設置焦點)
3.設置偵聽器:視圖允許客戶端設置偵聽器這將通知當一些有趣的事情發生的。
例如,所有視圖將允許您設置偵聽器在視圖時通知得失焦點。您可以使用這樣的監聽器註冊
{“鏈接# setonfocuschangelistener(Android。視圖。視圖。OnFocusChangeListener)}。
其他視圖子類提供更專門的偵聽器。例如,一個按鈕當單擊按鈕時,將偵聽器通知客戶端通知客戶端
(主要講view可以和用戶實現互動,並監聽各種事件,並且將了一個例子)。
4.設置可見 setVisibility(int)
* <p><em>
* 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}.
* </em></p>
Android框架負責測量、佈局和繪製視圖。你不應該調用方法執行這些方法的意見除非你自己實現了ViewGroup.(主要說 Android框架會自己實現 view的測量 佈局和繪製)。
* <a name="Lifecycle"></a>
* <h3>Implementing a Custom View</h3>
聲明週期 實現自定義View
* <p>
* 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)}.
* <table border="2" width="85%" align="center" cellpadding="5">
* <thead>
* <tr><th>Category</th> <th>Methods</th> <th>Description</th></tr>
* </thead>
*
* <tbody>
* <tr>
* <td rowspan="2">Creation</td>
* <td>Constructors</td>
* <td>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.
* </td>
* </tr>
* <tr>
* <td><code>{@link #onFinishInflate()}</code></td>
* <td>Called after a view and all of its children has been inflated
* from XML.</td>
* </tr>
*
* <tr>
* <td rowspan="3">Layout</td>
* <td><code>{@link #onMeasure(int, int)}</code></td>
* <td>Called to determine the size requirements for this view and all
* of its children.
* </td>
* </tr>
* <tr>
* <td><code>{@link #onLayout(boolean, int, int, int, int)}</code></td>
* <td>Called when this view should assign a size and position to all
* of its children.
* </td>
* </tr>
* <tr>
* <td><code>{@link #onSizeChanged(int, int, int, int)}</code></td>
* <td>Called when the size of this view has changed.
* </td>
* </tr>
*
* <tr>
* <td>Drawing</td>
* <td><code>{@link #onDraw(android.graphics.Canvas)}</code></td>
* <td>Called when the view should render its content.
* </td>
* </tr>
*
* <tr>
* <td rowspan="4">Event processing</td>
* <td><code>{@link #onKeyDown(int, KeyEvent)}</code></td>
* <td>Called when a new hardware key event occurs.
* </td>
* </tr>
* <tr>
* <td><code>{@link #onKeyUp(int, KeyEvent)}</code></td>
* <td>Called when a hardware key up event occurs.
* </td>
* </tr>
* <tr>
* <td><code>{@link #onTrackballEvent(MotionEvent)}</code></td>
* <td>Called when a trackball motion event occurs.
* </td>
* </tr>
* <tr>
* <td><code>{@link #onTouchEvent(MotionEvent)}</code></td>
* <td>Called when a touch screen motion event occurs.
* </td>
* </tr>
*
* <tr>
* <td rowspan="2">Focus</td>
* <td><code>{@link #onFocusChanged(boolean, int, android.graphics.Rect)}</code></td>
* <td>Called when the view gains or loses focus.
* </td>
* </tr>
*
* <tr>
* <td><code>{@link #onWindowFocusChanged(boolean)}</code></td>
* <td>Called when the window containing the view gains or loses focus.
* </td>
* </tr>
*
* <tr>
* <td rowspan="3">Attaching</td>
* <td><code>{@link #onAttachedToWindow()}</code></td>
* <td>Called when the view is attached to a window.
* </td>
* </tr>
*
* <tr>
* <td><code>{@link #onDetachedFromWindow}</code></td>
* <td>Called when the view is detached from its window.
* </td>
* </tr>
*
* <tr>
* <td><code>{@link #onWindowVisibilityChanged(int)}</code></td>
* <td>Called when the visibility of the window containing the view
* has changed.
* </td>
* </tr>
* </tbody>
*
* </table>
* </p>
實現一個自定義視圖,通常你會開始覆蓋框架調用所有視圖的一些標準方法。你做的不需要重寫所有這些方法。事實上,你可以從開始重寫{@link #onDraw(android.graphics.Canvas}開始。 (主要講自定義的View補兵重寫View的所有方法,你可以從onDraw()方法開始練習。(onDraw()負責的是View 的繪製)). ![這裏寫圖片描述](https://img-blog.csdn.net/20170306141819863?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXFfMjg4NjU5ODk=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast) 以上是對上一段源發的翻譯。
<a name="IDs"></a>
* <h3>IDs</h3>
* 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:
Id: 視圖可能具有與它們相關聯的整數id。這些IDS通常在佈局中指定xml文件,並用於查找特定的內部視圖視樹
常見的模式有:
* <ul>
* <li>Define a Button in the layout file and assign it a unique ID.
* <pre>
* <Button
* android:id="@+id/my_button"
* android:layout_width="wrap_content"
* android:layout_height="wrap_content"
* android:text="@string/my_button_text"/>
* </pre></li>
* <li>From the onCreate method of an Activity, find the Button
* <pre class="prettyprint">
* Button myButton = (Button) findViewById(R.id.my_button);
* </pre></li>
* </ul>
* <p>
* 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.
* </p>
以上講了一個例子講了一下Id的用法
* <a name="Position"></a>
* <h3>Position</h3>
* <p>
* The geometry of a view is that of a rectangle. A view has a location,
* expressed as a pair of <em>left</em> and <em>top</em> coordinates, and
* two dimensions, expressed as a width and a height. The unit for location
* and dimensions is the pixel.
* </p>
*
* <p>
* It is possible to retrieve the location of a view by invoking the methods
* {@link #getLeft()} and {@link #getTop()}. The former returns the left, or X,
* coordinate of the rectangle representing the view. The latter returns the
* top, or Y, coordinate of the rectangle representing the view. These methods
* both return the location of the view relative to its parent. For instance,
* when getLeft() returns 20, that means the view is located 20 pixels to the
* right of the left edge of its direct parent.
* </p>
*
* <p>
* In addition, several convenience methods are offered to avoid unnecessary
* computations, namely {@link #getRight()} and {@link #getBottom()}.
* These methods return the coordinates of the right and bottom edges of the
* rectangle representing the view. For instance, calling {@link #getRight()}
* is similar to the following computation: <code>getLeft() + getWidth()</code>
* (see <a href="#SizePaddingMargins">Size</a> for more information about the width.)
* </p>
*
Position:
視圖的幾何形狀是矩形的。視圖有位置,表示爲左和頂部座標,和以寬度和高度表示的兩個維度。定位單元尺寸是像素
通過調用方法獲取他的相對位置。前者返回表示視圖的矩形的左、或x座標。後者返回表示視圖的矩形的頂部或y座標。 這些方法都返回視圖相對於其父的位置。例如,當getleft()返回20,這意味着觀點是20像素右父左邊右。
此外,一些方便的方法,以避免不必要的計算,即{ getright() }和{ getbottom() }。這些方法返回的右邊緣和底部邊緣的座標表示視圖的矩形。例如,調用{getright() }類似於下面的計算:getLeft() + getWidth()。
* <a name="SizePaddingMargins"></a>
* <h3>Size, padding and margins</h3>
* <p>
* The size of a view is expressed with a width and a height. A view actually
* possess two pairs of width and height values.
* </p>
*
* <p>
* The first pair is known as <em>measured width</em> and
* <em>measured height</em>. These dimensions define how big a view wants to be
* within its parent (see <a href="#Layout">Layout</a> for more details.) The
* measured dimensions can be obtained by calling {@link #getMeasuredWidth()}
* and {@link #getMeasuredHeight()}.
* </p>
*
* <p>
* The second pair is simply known as <em>width</em> and <em>height</em>, or
* sometimes <em>drawing width</em> and <em>drawing height</em>. These
* dimensions define the actual size of the view on screen, at drawing time and
* after layout. These values may, but do not have to, be different from the
* measured width and height. The width and height can be obtained by calling
* {@link #getWidth()} and {@link #getHeight()}.
* </p>
*
* <p>
* To measure its dimensions, a view takes into account its padding. The padding
* is expressed in pixels for the left, top, right and bottom parts of the view.
* Padding can be used to offset the content of the view by a specific amount of
* pixels. For instance, a left padding of 2 will push the view's content by
* 2 pixels to the right of the left edge. Padding can be set using the
* {@link #setPadding(int, int, int, int)} or {@link #setPaddingRelative(int, int, int, int)}
* method and queried by calling {@link #getPaddingLeft()}, {@link #getPaddingTop()},
* {@link #getPaddingRight()}, {@link #getPaddingBottom()}, {@link #getPaddingStart()},
* {@link #getPaddingEnd()}.
* </p>
*
* <p>
* Even though a view can define a padding, it does not provide any support for
* margins. However, view groups provide such a support. Refer to
* {@link android.view.ViewGroup} and
* {@link android.view.ViewGroup.MarginLayoutParams} for further information.
* </p>
Size ,Padding和Margin:
視圖的大小用寬度和高度表示。實際上具有兩對寬度和高度值
第一對被稱爲測量寬度和測量高度 這些維度定義了一個視圖要多大在其父母測量的尺寸可以通過調用 測量寬度和測量高度可以調用 {getMeasuredWidth()和getMeasuredHeight()}
第二對是簡單的稱爲寬度和高度,有時稱圖紙寬度和繪圖高度。這些維度定義屏幕上的視圖的實際大小,在繪圖時間和佈局後。這些值可以,但不必,不同於測得的寬度和高度。寬度和高度可以通過調用{ getwidth() }和{getheight() }。
Padding 爲了測量它的尺寸,一個視圖考慮到它的填充。填料以視圖的上、下、左和右部分的像素表示。填充可以用來抵消視圖的具體內容像素。例如,一個2的左填充將推動視圖的內容左邊緣右2像素。可以使用 {setpadding(int,int,int,int)}或{setpaddingrelative(int,int,int,int)}方法和查詢調用{getpaddingleft() },{getpaddingtop() },{getpaddingright() },{ getpaddingbottom() },{ getpaddingstart() }, {getpaddingend() }。
Margin 即使視圖可以定義填充,它也不提供任何支持Margin 。但是,ViewGroup提供這樣的支持。參閱{@鏈接ViewGroup }的{ ViewGroup.marginlayoutparams }。(View支持padding 但是不支持Margin ViewGroup支持Margin 因爲ViewGroup內部有一個Marginlayoutparams )
* <a name="Layout"></a>
* <h3>Layout</h3>
* <p>
* Layout is a two pass process: a measure pass and a layout pass. The measuring
* pass is implemented in {@link #measure(int, int)} and is a top-down traversal
* of the view tree. Each view pushes dimension specifications down the tree
* during the recursion. At the end of the measure pass, every view has stored
* its measurements. The second pass happens in
* {@link #layout(int,int,int,int)} and is also top-down. During
* this pass each parent is responsible for positioning all of its children
* using the sizes computed in the measure pass.
* </p>
*
* <p>
* When a view's measure() method returns, its {@link #getMeasuredWidth()} and
* {@link #getMeasuredHeight()} values must be set, along with those for all of
* that view's descendants. A view's measured width and measured height values
* must respect the constraints imposed by the view's parents. This guarantees
* that at the end of the measure pass, all parents accept all of their
* children's measurements. A parent view may call measure() more than once on
* its children. For example, the parent may measure each child once with
* unspecified dimensions to find out how big they want to be, then call
* measure() on them again with actual numbers if the sum of all the children's
* unconstrained sizes is too big or too small.
* </p>
*
* <p>
* The measure pass uses two classes to communicate dimensions. The
* {@link MeasureSpec} class is used by views to tell their parents how they
* want to be measured and positioned. The base LayoutParams class just
* describes how big the view wants to be for both width and height. For each
* dimension, it can specify one of:
* <ul>
* <li> an exact number
* <li>MATCH_PARENT, which means the view wants to be as big as its parent
* (minus padding)
* <li> WRAP_CONTENT, which means that the view wants to be just big enough to
* enclose its content (plus padding).
* </ul>
* There are subclasses of LayoutParams for different subclasses of ViewGroup.
* For example, AbsoluteLayout has its own subclass of LayoutParams which adds
* an X and Y value.
* </p>
*
* <p>
* MeasureSpecs are used to push requirements down the tree from parent to
* child. A MeasureSpec can be in one of three modes:
* <ul>
* <li>UNSPECIFIED: This is used by a parent to determine the desired dimension
* of a child view. For example, a LinearLayout may call measure() on its child
* with the height set to UNSPECIFIED and a width of EXACTLY 240 to find out how
* tall the child view wants to be given a width of 240 pixels.
* <li>EXACTLY: This is used by the parent to impose an exact size on the
* child. The child must use this size, and guarantee that all of its
* descendants will fit within this size.
* <li>AT_MOST: This is used by the parent to impose a maximum size on the
* child. The child must guarantee that it and all of its descendants will fit
* within this size.
* </ul>
* </p>
*
* <p>
* To intiate a layout, call {@link #requestLayout}. This method is typically
* called by a view on itself when it believes that is can no longer fit within
* its current bounds.
* </p>
重點:
《測量》第二步驟發生在{lay佈局(int,int,int,int)}也是自上而下的。在這一關每個父View都負責定位其所有的子View使用測量中計算的尺寸。
當一個視圖的measure()方法返回時,它 getmeasuredwidth() }{getmeasuredheight() }值必須設置,連同所有View的後代都設定了。視圖的測量寬度和測量高度值必須遵守父View強加的限制。這保證了在測量結束時,所有的父View接受他們的所有子View測量。父視圖可以不止一次調用measure()去測量子View。例如,家長可以測量每個孩子一次未指定的尺寸,以找出他們想多大,如果所有子View他們measure()無約束尺寸或者過大或過小,然後調用實際數字。 (當一個View調用measure()方法返回時,必須設置getmeasuredwidth() 和getmeasuredheight()連同所有View的後代都設定了。View的測量寬度和測量高度值必須遵守父View強加的限制 ,這保證了在測量結束時,所有的父View接受他們的所有子View測量。父視圖可以不止一次調用measure()去測量子View。例如,家長可以測量每個孩子一次未指定的 ,以找出他們想多大,如果所有子View他們measure()無約束尺寸或者過大或過小,然後調用實際數字。 這裏其實已經講了measure()方法)
度量傳遞使用兩個類來傳遞維度。這個{measurespec }類使用的告訴父容器如何想要被測量和定位。LayoutParams 類 參數類描述視圖的寬度和高度要有多大。對於每個 尺寸,它可以指定一個測量模式:
精確數字 MATCH_PARENT :這意味着這個視圖要和它的父容器一樣大(減去paddding) WRAP_CONTENT:這意味着該視圖只想足夠大的區域去包裹內容 有不同的 ViewGroup LayoutParams的子類。 例如,AbsoluteLayout包含LayoutParams子類擁有一個X和Y值。
MeasureSpecs 用來將測量需求從父容器傳給子View。 一個測量模式可以是一下的三種模式之一
UNSPECIFIED是未指定尺寸,這種情況不多,一般都是父控件是AdapterView,通過measure方法傳入的模式 EXACTLY是指精確尺寸: 當我們將控件的layout_width或layout_height指定爲具體數值時如andorid:layout_width=”50dip”,或者爲FILL_PARENT是,都是控件大小已經確定的情況,都是精確尺寸 AT_MOS是最大尺寸:當控件的layout_width或layout_height指定爲WRAP_CONTENT時,控件大小一般隨着控件的子空間或內容進行變化,此時控件尺寸只要不超過父控件允許的最大尺寸即可。因此,此時的mode是AT_MOST,size給出了父控件允許的最大尺寸
要初始化一個佈局,調用{requestLayout方法}。當它認爲不能在當前範圍內適應時,該方法通常被視圖本身調用.(我們可以理解爲重新佈局了一下。 當view確定自身已經不再適合現有的區域時,該view本身調用這個方法要求parent view重新調用他onMeasure onLayout來對重新設置自己位置。特別的當view的layoutparameter發生改變,並且它的值還沒能應用到view上,這時候適合調用這個方法。)
* <a name="Drawing"></a>
* <h3>Drawing</h3>
* <p>
* Drawing is handled by walking the tree and recording the drawing commands of
* any View that needs to update. After this, the drawing commands of the
* entire tree are issued to screen, clipped to the newly damaged area.
* </p>
*
* <p>
* The tree is largely recorded and drawn in order, with parents drawn before
* (i.e., behind) their children, with siblings drawn in the order they appear
* in the tree. If you set a background drawable for a View, then the View will
* draw it before calling back to its <code>onDraw()</code> method. The child
* drawing order can be overridden with
* {@link ViewGroup#setChildrenDrawingOrderEnabled(boolean) custom child drawing order}
* in a ViewGroup, and with {@link #setZ(float)} custom Z values} set on Views.
* </p>
*
* <p>
* To force a view to draw, call {@link #invalidate()}.
* </p>
重點
《繪製》Draw 是通過在View樹上行走並記錄需要更新的視圖的繪製命令。在此之後,繪製命令的整棵View樹被髮到屏幕,裁剪到新損壞的區域。(調用view的繪繪製方法會繪製View和剪裁View)
View的繪製在很大程度上是按順序記錄和繪製的,之前是父母畫的 (即,落後)他們的孩子,兄弟姐妹按照他們出現的順序 在樹上。如果你設定了一個背景的一個視圖,然後將繪製自己在調用 onDraw() 將之前。孩子繪圖順序可以覆蓋{ViewGroup setchildrendrawingorderenabled(boolean)定製子View繪畫秩序} 在一個ViewGroup,與{setz(float)} }自定義Z值設置在View上。
調用view繪製的方法invalidate() View本身調用迫使view重畫。
*
* <a name="EventHandlingThreading"></a>
* <h3>Event Handling and Threading</h3>
* <p>
* The basic cycle of a view is as follows:
* <ol>
* <li>An event comes in and is dispatched to the appropriate view. The view
* handles the event and notifies any listeners.</li>
* <li>If in the course of processing the event, the view's bounds may need
* to be changed, the view will call {@link #requestLayout()}.</li>
* <li>Similarly, if in the course of processing the event the view's appearance
* may need to be changed, the view will call {@link #invalidate()}.</li>
* <li>If either {@link #requestLayout()} or {@link #invalidate()} were called,
* the framework will take care of measuring, laying out, and drawing the tree
* as appropriate.</li>
* </ol>
* </p>
*
* <p><em>Note: The entire view tree is single threaded. You must always be on
* the UI thread when calling any method on any view.</em>
* If you are doing work on other threads and want to update the state of a view
* from that thread, you should use a {@link Handler}.
* </p>
《事件處理和線程》
view 的循環基本如下:
一個事件進來並被髮送到適當的視圖。視圖處理該事件並通知listeners,如果在處理事件的過程中,視圖的邊界可能需要要改變,視圖將調用{requestlayout() } 同樣,如果在處理事件的過程中視圖的外觀可能需要改變,視圖將調用{ invalidate() } 如果 requestlayout() }或{ invalidate() }被調用, 該框架將在適當的時候託管 測量,排版,並繪製View樹。
注意:整個視圖樹是單線程的。在任何視圖上調用任何方法時的必須是在UI線程上。 如果你在工作線程希望更新視圖的狀態 從該線程中,你可以使用handler。
*
* <a name="FocusHandling"></a>
* <h3>Focus Handling</h3>
* <p>
* The framework will handle routine focus movement in response to user input.
* This includes changing the focus as views are removed or hidden, or as new
* views become available. Views indicate their willingness to take focus
* through the {@link #isFocusable} method. To change whether a view can take
* focus, call {@link #setFocusable(boolean)}. When in touch mode (see notes below)
* views indicate whether they still would like focus via {@link #isFocusableInTouchMode}
* and can change this via {@link #setFocusableInTouchMode(boolean)}.
* </p>
* <p>
* Focus movement is based on an algorithm which finds the nearest neighbor in a
* given direction. In rare cases, the default algorithm may not match the
* intended behavior of the developer. In these situations, you can provide
* explicit overrides by using these XML attributes in the layout file:
* <pre>
* nextFocusDown
* nextFocusLeft
* nextFocusRight
* nextFocusUp
* </pre>
* </p>
*
*
* <p>
* To get a particular view to take focus, call {@link #requestFocus()}.
* </p>
《焦點處理》
框架將處理日常焦點運動響應用戶輸入。這包括改變焦點的視圖被刪除或隱藏,或作爲新的視圖變得可用。View繪製後可以通過{isFocusable()}獲取焦點。設置View室友擁有焦點,叫{ setfocusable(boolean)}. 當在觸摸模式下可以通過(isfocusableintouchmode()方法)表名View是否擁有焦點。調用(setfocusableintouchmode(boolean)方法)可以改變view的焦點。
焦點運動是基於一個算法,發現最近 給定命令的鄰居。在極少數情況下,默認算法可能不匹配 開發者的預期行爲。在這些情況下,可以在佈局文件中使用以下的屬性:
** nextFocusDown * nextFocusLeft * nextFocusRight * nextFocusUp *
一些特別的View調用 {requestFocus()} 何以獲取焦點
* <a name="TouchMode"></a>
* <h3>Touch Mode</h3>
* <p>
* When a user is navigating a user interface via directional keys such as a D-pad, it is
* necessary to give focus to actionable items such as buttons so the user can see
* what will take input. If the device has touch capabilities, however, and the user
* begins interacting with the interface by touching it, it is no longer necessary to
* always highlight, or give focus to, a particular view. This motivates a mode
* for interaction named 'touch mode'.
* </p>
* <p>
* For a touch capable device, once the user touches the screen, the device
* will enter touch mode. From this point onward, only views for which
* {@link #isFocusableInTouchMode} is true will be focusable, such as text editing widgets.
* Other views that are touchable, like buttons, will not take focus when touched; they will
* only fire the on click listeners.
* </p>
* <p>
* Any time a user hits a directional key, such as a D-pad direction, the view device will
* exit touch mode, and find a view to take focus, so that the user may resume interacting
* with the user interface without touching the screen again.
* </p>
* <p>
* The touch mode state is maintained across {@link android.app.Activity}s. Call
* {@link #isInTouchMode} to see whether the device is currently in touch mode.
* </p>
《觸摸模式》
用戶界面將通過方向鍵如 D-pad 給用戶指引,有必要給用戶操作的條目上給上焦點例如,button,這樣用戶就明白將要輸入什麼。如果設備具有觸摸功能,不管怎樣,當用戶通過界面發生交互時,它就沒有必要是高亮的,或者繼續擁有焦點,或者是一個特殊的View.這種互動模式命名爲“觸摸模式”。 對於這種可觸摸的設備,當觸摸屏幕的時候,設備就是觸摸模式。從這點起,只有獲得焦點的控件{#isFocusableInTouchMode}是true ru:可編輯的文本。
其他的View是可觸摸的,如按鈕,觸摸時不帶焦點;他們只會觸發Click監聽。
任何時候一個用戶點擊一個方向鍵,如方向鍵方向,查看裝置將退出觸摸模式,並找到一個視圖獲取焦點,這樣用戶可以恢復與用戶界面的交互無需再次觸摸屏幕
觸摸模式的狀態可以通過{{ android.app.Activity}}調用{isInTouchMode)}獲取設備的當前觸摸模式。
* <a name="Scrolling"></a>
* <h3>Scrolling</h3>
* <p>
* The framework provides basic support for views that wish to internally
* scroll their content. This includes keeping track of the X and Y scroll
* offset as well as mechanisms for drawing scrollbars. See
* {@link #scrollBy(int, int)}, {@link #scrollTo(int, int)}, and
* {@link #awakenScrollBars()} for more details.
* </p>
《滑動》
框架提供一些基本的內部自動滑動的View。這包括跟蹤x和y滾動 偏移以及繪製滾動機制。 See * {@link #scrollBy(int, int)}, {@link #scrollTo(int, int)}, and * {@link #awakenScrollBars()} for more details. * 請查看scrollBy(int, int),scrollTo(int, int)和awakenScrollBars() 獲取更多的信息。
* <a name="Tags"></a>
* <h3>Tags</h3>
* <p>
* Unlike IDs, tags are not used to identify views. Tags are essentially an
* extra piece of information that can be associated with a view. They are most
* often used as a convenience to store data related to views in the views
* themselves rather than by putting them in a separate structure.
* </p>
《標籤》
與IDS不同,標籤不用於識別視圖。標籤本質上是 額外的信息,可以與視圖關聯。他們是最 通常用於方便存儲與視圖中的視圖相關的數據 而不是把它們放在一個單獨的結構中。
* <a name="Properties"></a>
* <h3>Properties</h3>
* <p>
* The View class exposes an {@link #ALPHA} property, as well as several transform-related
* properties, such as {@link #TRANSLATION_X} and {@link #TRANSLATION_Y}. These properties are
* available both in the {@link Property} form as well as in similarly-named setter/getter
* methods (such as {@link #setAlpha(float)} for {@link #ALPHA}). These properties can
* be used to set persistent state associated with these rendering-related properties on the view.
* The properties and methods can also be used in conjunction with
* {@link android.animation.Animator Animator}-based animations, described more in the
* <a href="#Animation">Animation</a> section.
* </p>
* <a name="Tags"></a>
* <h3>Tags</h3>
* <p>
* Unlike IDs, tags are not used to identify views. Tags are essentially an
* extra piece of information that can be associated with a view. They are most
* often used as a convenience to store data related to views in the views
* themselves rather than by putting them in a separate structure.
* </p>
* <p>
* Tags may be specified with character sequence values in layout XML as either
* a single tag using the {@link android.R.styleable#View_tag android:tag}
* attribute or multiple tags using the {@code <tag>} child element:
* <pre>
* <View ...
* android:tag="@string/mytag_value" />
* <View ...>
* <tag android:id="@+id/mytag"
* android:value="@string/mytag_value" />
* </View>
* </pre>
* </p>
* <p>
* Tags may also be specified with arbitrary objects from code using
* {@link #setTag(Object)} or {@link #setTag(int, Object)}.
* </p>
《Tag 標籤》
與IDS不同,標籤不用於識別視圖。標籤本質上是 額外的信息,可以與視圖關聯。他們是最 通常用於方便存儲與視圖中的視圖相關的數據 而不是把它們放在一個單獨的結構中。(本質就是view存儲數據 的結構)
標籤可以在XML佈局中使用字符序列值指定爲使用屬性,或使用多個子元素的爲其tag的值 (具體可以看一下 ListView 的Adapter 中的 ViewHolder 的使用 )
標籤也可以指定任意對象 { settag(object)}或{ settag(int,object)}。
* <a name="Themes"></a>
* <h3>Themes</h3>
* <p>
* By default, Views are created using the theme of the Context object supplied
* to their constructor; however, a different theme may be specified by using
* the {@link android.R.styleable#View_theme android:theme} attribute in layout
* XML or by passing a {@link ContextThemeWrapper} to the constructor from
* code.
* </p>
* <p>
* When the {@link android.R.styleable#View_theme android:theme} attribute is
* used in XML, the specified theme is applied on top of the inflation
* context's theme (see {@link LayoutInflater}) and used for the view itself as
* well as any child elements.
* </p>
* <p>
* In the following example, both views will be created using the Material dark
* color scheme; however, because an overlay theme is used which only defines a
* subset of attributes, the value of
* {@link android.R.styleable#Theme_colorAccent android:colorAccent} defined on
* the inflation context's theme (e.g. the Activity theme) will be preserved.
* <pre>
* <LinearLayout
* ...
* android:theme="@android:theme/ThemeOverlay.Material.Dark">
* <View ...>
* </LinearLayout>
* </pre>
* </p>
*
《主題 Themes》
默認情況下,使用Context的主題創建視圖他們的構造函數或者可以使用指定的不同主題。 這個{@link android.R.styleable#View_theme android:theme}XML屬性或通過{@link ContextThemeWrapper}的構造代碼。
當在xml中指定屬性{@link android.R.styleable#View_theme android:theme},指定的主題應用於Context主題(LayoutInflater })和用於自己以及任何子元素。
使用的View使用 Material dark 主題,請參照下面的例子 :
Theme 屬性是 在Android 5.0 之後推出來 。view上直接使用主題的應用場景不是很多,大家要用的話推薦使用 stytle屬性。
* <a name="Properties"></a>
* <h3>Properties</h3>
* <p>
* The View class exposes an {@link #ALPHA} property, as well as several transform-related
* properties, such as {@link #TRANSLATION_X} and {@link #TRANSLATION_Y}. These properties are
* available both in the {@link Property} form as well as in similarly-named setter/getter
* methods (such as {@link #setAlpha(float)} for {@link #ALPHA}). These properties can
* be used to set persistent state associated with these rendering-related properties on the view.
* The properties and methods can also be used in conjunction with
* {@link android.animation.Animator Animator}-based animations, described more in the
* <a href="#Animation">Animation</a> section.
* </p>
《屬性 Properties 》
View 中有很多的屬性,可以 查看他們的get()和set() 方法。這些屬性可以 用於在veiw上設置與呈現相關屬性相關聯的持久狀態. 這些屬性可以和動畫配合使用。
* <a name="Animation"></a>
* <h3>Animation</h3>
* <p>
* Starting with Android 3.0, the preferred way of animating views is to use the
* {@link android.animation} package APIs. These {@link android.animation.Animator Animator}-based
* classes change actual properties of the View object, such as {@link #setAlpha(float) alpha} and
* {@link #setTranslationX(float) translationX}. This behavior is contrasted to that of the pre-3.0
* {@link android.view.animation.Animation Animation}-based classes, which instead animate only
* how the view is drawn on the display. In particular, the {@link ViewPropertyAnimator} class
* makes animating these View properties particularly easy and efficient.
* </p>
* <p>
* Alternatively, you can use the pre-3.0 animation classes to animate how Views are rendered.
* You can attach an {@link Animation} object to a view using
* {@link #setAnimation(Animation)} or
* {@link #startAnimation(Animation)}. The animation can alter the scale,
* rotation, translation and alpha of a view over time. If the animation is
* attached to a view that has children, the animation will affect the entire
* subtree rooted by that node. When an animation is started, the framework will
* take care of redrawing the appropriate views until the animation completes.
* </p>
《動畫 Animation》
從Android 3.0 開始,動畫視圖的首選是{@link android.animation}包中的 APIs. 這些{{@link android.animation.Animator Animator}基類改變view的實際屬性 例如: {@link #setAlpha(float) alpha} and{@link #setTranslationX(float) translationX}.這些行爲在3.0後已經被{@link android.view.animation.Animation Animation}代替。特別是,{@link ViewPropertyAnimator} 類 使屬性動畫變得更高效和容易。
你可以在view上使用{setAnimation(Animation)}或者{startAnimation(Animation)}去展示一個動畫。 動畫可以在一段時間後使view 平移 旋轉 放大 透明度改變
如果一個字View 上的動畫,這個動畫將會影響這個節點下的所有 View 樹。底層將會刷新這個view知道動畫結束。
* <a name="Security"></a>
* <h3>Security</h3>
* <p>
* Sometimes it is essential that an application be able to verify that an action
* is being performed with the full knowledge and consent of the user, such as
* granting a permission request, making a purchase or clicking on an advertisement.
* Unfortunately, a malicious application could try to spoof the user into
* performing these actions, unaware, by concealing the intended purpose of the view.
* As a remedy, the framework offers a touch filtering mechanism that can be used to
* improve the security of views that provide access to sensitive functionality.
* </p><p>
* To enable touch filtering, call {@link #setFilterTouchesWhenObscured(boolean)} or set the
* android:filterTouchesWhenObscured layout attribute to true. When enabled, the framework
* will discard touches that are received whenever the view's window is obscured by
* another visible window. As a result, the view will not receive touches whenever a
* toast, dialog or other window appears above the view's window.
* </p><p>
* For more fine-grained control over security, consider overriding the
* {@link #onFilterTouchEventForSecurity(MotionEvent)} method to implement your own
* security policy. See also {@link MotionEvent#FLAG_WINDOW_IS_OBSCURED}.
* </p>
《安全 Security》
有時,一個應用程序能夠驗證一個動作執行必須是用戶同意且充分認識是非常必要的 如授予許可請求,購買或點擊廣告。 不幸的是,一個惡意應用程序可能試圖欺騙用戶通過隱藏視圖目的來執行這些動作。 作爲補救措施,該框架提供了一個觸摸過濾機制,可用於提高提供敏感功能訪問的視圖的安全性。
過濾觸摸,調用 {@link #setFilterTouchesWhenObscured(boolean)}或者在佈局文件中 設置 android:filterTouchesWhenObscured 屬性爲true 啓用時,每當視圖窗口被另一可見窗口遮擋時框架 將丟棄所接收到的這個 view的觸摸。 因此,當toast 對話框或者其他 的窗口出現時 這個view將不接收觸摸事件。
對於更細粒度的安全控制,考慮重寫 { onfiltertoucheventforsecurity(MotionEvent)}方法來實現自己的 安全策略。參閱{@link MotionEvent#FLAG_WINDOW_IS_OBSCURED }
* @attr ref android.R.styleable#View_alpha//關聯方法: setAlpha(float) 屬性說明: 視圖透明度,值在0-1之間。0爲完全透明,1爲完全不透明。
* @attr ref android.R.styleable#View_background關聯方法: setBackgroundResource(int) 屬性說明: 視圖背景
* @attr ref android.R.styleable#View_clickable 關聯方法: setClickable(boolean) 屬性說明: 視圖是否可點擊
* @attr ref android.R.styleable#View_contentDescription// 對於一些視力有障礙的用戶,android提供給用戶一個很有用的功能,可以幫助使用app。
這個屬性必須在用戶的Accessible 中的相應屬性開啓後才能使用。點擊Accessible控件,android系統會自動使用人聲朗讀控件上android:contentDescription屬性說指向的內容。
這個屬性的主要功能就是爲視力有障礙的人增加對控件的解釋。
* @attr ref android.R.styleable#View_drawingCacheQuality// 關聯方法: setDrawingCacheQuality(int) 屬性說明: "設置繪圖時半透明質量。有可以取以下3個值 auto——默認,由框架決定 high——高質量,使用較高的顏色深度,消耗更多的內存 low——低質量,使用較低的顏色深度,但是用更少的內存"
* @attr ref android.R.styleable#View_duplicateParentState//關聯方法: 屬性說明: 如果設置此屬性,將直接從父容器中獲取繪圖狀態(光標,按下等)
* @attr ref android.R.styleable#View_id//關聯方法: setId(int) 屬性說明: 給當前View設置一個在當前layout.xml中的唯一編號,可以通過調用View.findViewById() 或Activity.findViewById()根據這個編號查找到對應的View。不同的layout.xml之間定義相同的id不會衝突。
* @attr ref android.R.styleable#View_requiresFadingEdge // 關聯方法: setVerticalFadingEdgeEnabled(boolean) 屬性說明: 定義滾動時邊緣是否褪色
* @attr ref android.R.styleable#View_fadeScrollbars //關聯方法: setScrollbarFadingEnabled(boolean) 屬性說明: 定義在ScrollBar沒有使用時,是否褪色。
* @attr ref android.R.styleable#View_fadingEdgeLength 關聯方法: getVerticalFadingEdgeLength() 屬性說明: 設置邊框漸變的長度。
* @attr ref android.R.styleable#View_filterTouchesWhenObscured //關聯方法:setFilterTouchesWhenObscured(boolean) 屬性說明: view所在窗口被其它可見窗口遮住時,是否過濾觸摸事件。
* @attr ref android.R.styleable#View_fitsSystemWindows // 關聯方法: setFitsSystemWindows(boolean) 屬性說明: 設置佈局調整時是否考慮系統窗口(如狀態欄)
* @attr ref android.R.styleable#View_isScrollContainer
* @attr ref android.R.styleable#View_focusable // 關聯方法: setFocusable(boolean) 屬性說明: 設置是否獲得焦點。若有requestFocus()被調用時,後者優先處理。注意在表單中想設置某一個如EditText獲取焦點,光設置這個是不行的,需要將這個EditText前面的focusable都設置爲false才行。在Touch模式下獲取焦點需要設置focusableInTouchMode爲true。
* @attr ref android.R.styleable#View_focusableInTouchMode // 關聯方法: setFocusableInTouchMode(boolean) 屬性說明: 設置在Touch模式下View是否能取得焦點。
* @attr ref android.R.styleable#View_hapticFeedbackEnabled 關聯方法: setHapticFeedbackEnabled(boolean) 屬性說明: 是否啓用觸摸反饋,啓用後就是在點擊等操作時會有震動等反饋效果
* @attr ref android.R.styleable#View_keepScreenOn //關聯方法: setKeepScreenOn(boolean) 屬性說明: 視圖在可見的情況下是否保持喚醒狀態。
* @attr ref android.R.styleable#View_layerType // 關聯方法: setLayerType(int,Paint) 屬性說明: "設置指定層的類型,可以取以下3個值: none——不指定 software——軟件層。 hardware——硬件層。使用硬件加速。
* @attr ref android.R.styleable#View_layoutDirection // 關聯方法: setLayoutDirection(int) 屬性說明: 定義佈局圖紙的方向
* @attr ref android.R.styleable#View_longClickable //關聯方法: setLongClickable(boolean) 屬性說明: 是否響應長點擊事件
* @attr ref android.R.styleable#View_minHeight //關聯方法: setMinimumHeight(int) 屬性說明: 設置視圖最小高度
* @attr ref android.R.styleable#View_minWidth//關聯方法: setMinimumWidth(int) 屬性說明: 設置視圖最小寬度
* @attr ref android.R.styleable#View_nextFocusDown// 關聯方法: setNextFocusDownId(int) 屬性說明: 向下移動焦點時,下一個獲取焦點的view的id
* @attr ref android.R.styleable#View_nextFocusLeft// 關聯方法: setNextFocusLeftId(int) 屬性說明: 向左移動焦點時,下一個獲取焦點的view的id
* @attr ref android.R.styleable#View_nextFocusRight//關聯方法: setNextFocusRightId(int) 屬性說明: 向右移動焦點時,下一個獲取焦點的view的id
* @attr ref android.R.styleable#View_nextFocusUp// 關聯方法: setNextFocusForwardId(int) 屬性說明: 向上移動焦點時,下一個獲取焦點的view的id
* @attr ref android.R.styleable#View_onClick//關聯方法: 屬性說明: 點擊時,要調用的方法的名稱。
* @attr ref android.R.styleable#View_padding// 關聯方法: setPaddingRelative(int,int,int,int) 屬性說明: 設置上下左右的邊距
* @attr ref android.R.styleable#View_paddingBottom//關聯方法: setPaddingRelative(int,int,int,int) 屬性說明: 下邊距
* @attr ref android.R.styleable#View_paddingLeft// 左邊距
* @attr ref android.R.styleable#View_paddingRight//右邊距
* @attr ref android.R.styleable#View_paddingTop//上邊距
* @attr ref android.R.styleable#View_paddingStart//相當於左邊距
* @attr ref android.R.styleable#View_paddingEnd//相當於右邊距
* @attr ref android.R.styleable#View_saveEnabled// 關聯方法: setSaveEnabled(boolean) 屬性說明: 在配置改變等情況出現時是否保存view的狀態數據。如果你的view有id,那默認系統就會幫你保存。
* @attr ref android.R.styleable#View_rotation//關聯方法: setRotation(float) 屬性說明: 旋轉度數
* @attr ref android.R.styleable#View_rotationX//關聯方法: setRotationX(float) 屬性說明: 水平旋轉度數
* @attr ref android.R.styleable#View_rotationY//關聯方法: setRotationY(float) 屬性說明: 豎直旋轉度數
* @attr ref android.R.styleable#View_scaleX//關聯方法: setScaleX(float) 屬性說明: 水平方向縮放比例
* @attr ref android.R.styleable#View_scaleY//關聯方法: setScaleY(float) 屬性說明: 豎直方向縮放比例
* @attr ref android.R.styleable#View_scrollX//關聯方法: 屬性說明: x方向的滾動偏移。即在水平方向滾動了多少距離
* @attr ref android.R.styleable#View_scrollY//關聯方法: 屬性說明: y方向的滾動偏移。即在豎直方向滾動了多少距離
* @attr ref android.R.styleable#View_scrollbarSize// 關聯方法: setScrollBarSize(int) 屬性說明: 設置滾動條的尺寸。垂直滾動條的寬度、水平滾動條的高度
* @attr ref android.R.styleable#View_scrollbarStyle// 關聯方法: setScrollBarStyle(int) 屬性說明: "滾動條的風格。共4組值: insideOverlay——內貼圖 insideInset——內插圖 outsideOverlay——外貼圖 outsideInset——外插圖。 inside就是滾動條在繪製在padding以內;outside就是不需要繪製在padding內(即view的邊界處);Overlay是貼圖,就是直接覆蓋在內容的上方,這樣內容可能會顯示到滾動條下方去;Inset是插圖,就是會在對應padding上加上滾動條的寬度,以不讓內容顯示到滾動條下面去。"
* @attr ref android.R.styleable#View_scrollbars// 關聯方法: 屬性說明: "設置可顯示的滾動條。有3個取值: none——不顯示滾動條 horizontal——顯示水平滾動條 vertical——顯示豎直滾動條"
* @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade//關聯方法: setScrollBarDefaultDelayBeforeFade(int) 屬性說明: 滾動條在n毫秒後開始淡出。
* @attr ref android.R.styleable#View_scrollbarFadeDuration// 關聯方法: setScrollBarFadeDuration(int) 屬性說明: 滾動條用多長時間淡出完畢
* @attr ref android.R.styleable#View_scrollbarTrackHorizontal// 關聯方法: 屬性說明: 是否總是繪製水平滾動條的滾動軌道
* @attr ref android.R.styleable#View_scrollbarThumbHorizontal//關聯方法: 屬性說明: 是否總是繪製豎直滾動條的滾動軌道
* @attr ref android.R.styleable#View_scrollbarThumbHorizontal // 關聯方法: 屬性說明: 水平滾動塊的drawable對象
* @attr ref android.R.styleable#View_scrollbarTrackVertical//關聯方法: 屬性說明: 豎直滾動條滾動軌道的drawable對象
* @attr ref android.R.styleable#View_scrollbarAlwaysDrawHorizontalTrack// 關聯方法: 屬性說明: 水平滾動條滾動軌道的drawable對象
* @attr ref android.R.styleable#View_scrollbarAlwaysDrawVerticalTrack//
* @attr ref android.R.styleable#View_stateListAnimator//
* @attr ref android.R.styleable#View_transitionName//
* @attr ref android.R.styleable#View_soundEffectsEnabled//
* @attr ref android.R.styleable#View_tag//關聯方法: 屬性說明: string標識。類似id,id是整數標識。
* @attr ref android.R.styleable#View_textAlignment// 關聯方法: setTextAlignment(int) 屬性說明: 設置文本的顯示方式。
* @attr ref android.R.styleable#View_textDirection// 關聯方法: setTextDirection(int) 屬性說明: 設置文本的顯示方向。
* @attr ref android.R.styleable#View_transformPivotX// 關聯方法: setPivotX(float) 屬性說明: 水平方向偏轉量
* @attr ref android.R.styleable#View_transformPivotY// 關聯方法: setPivotY(float) 屬性說明: 豎直方向偏轉量
* @attr ref android.R.styleable#View_translationX// 關聯方法: setTranslationX(float) 屬性說明: 水平方向的移動距離
* @attr ref android.R.styleable#View_translationY//關聯方法: setTranslationY(float) 屬性說明: 豎直方向的移動距離
* @attr ref android.R.styleable#View_translationZ//關聯方法: setTranslationY(float) 屬性說明: Z方向的移動距離
* @attr ref android.R.styleable#View_visibility//關聯方法: setVisibility(int) 屬性說明: "view的可見性。有3個取值: gone——不可見,同時不佔用view的空間; invisible——不可見,但佔用view的空間; visible——可見"
* @attr ref android.R.styleable#View_theme// 主題