Preface
Keywords: Android. View. viewstub, version: Android 2.2 r1
This chapter is translated fromTang MingHere, this blog is responsible for organizing and publishing, welcome other translators to participate in the android API Chinese Translation Action, contact me over140@gmail.com.
Statement
You are welcome to repost, but please keep the original source of the article :)
Blog: http://www.cnblogs.com
Farmer's uncle: http://www.cnblogs.com/over140/
Body
I. Structure
Public final class viewstub extends View
Java. Lang. Object
Android. View. View
Android. View. viewstub
Ii. Category Overview
Viewstub is a hidden view object that does not occupy memory space. It can load layout resource files at runtime delay. The layout resource file is loaded only when viewstub is visible or the inflate () function is called. This viewstub replaces itself in the parent container when loading the view. Therefore, viewstub will remain in the view until setvisibility (INT) or inflate () is called. The layout parameters of viewstub are added to the parent container of viewstub along with the number of loaded views. Similarly, you can use the inflatedid attribute to define or rename the id value of the view object to be loaded. For example:
You can use the "stud" ID to find the defined viewstub object. After the layout resource file "mysubtree" is loaded, the viewstub object is removed from its parent container. You can use ID "subtree" to find the view created by the layout resource "mysubtree. This view object is finally specified as a 120dip wide and a 40dip High. The recommended method for loading layout resource files is as follows:
When inflate () is called, The viewstub is replaced by the loaded view and the view object is returned. In this way, the application does not need to execute findviewbyid () to obtain reference for loading views.
(Translator's note: This class probably means to associate viewstub with the layout resource file specified in the XML file, so that the layout resource file can be loaded as needed. The main function is to optimize the performance. When to load the program, you don't need to load the program at the start, which can speed up the startup of the program and save memory resources .)
Iii. nesting
The viewstub. oninflatelistener interface is a listener that receives notifications that viewstub has successfully loaded the layout resource file.
Iv. xml attributes
Attribute name |
Related Methods |
Description |
Android: inflatedid |
Setinflatedid (INT) |
Overwrite the id value of the view to be loaded. |
Android: Layout |
Setlayoutresource (INT) |
Provides an identifier for the resource view to be loaded. It is used when viewstub becomes visible or gets focus. (Translator's note: ID of the layout resource file to be referenced) |
5. Constructor
Viewstub (context, int layoutresource)
Create a viewstub object associated with the specified layout resource file.
Parameters
ID of the layout resource file to be loaded by layoutresource.
Vi. Public Methods
Public void draw (canvas)
Manually draw this view (and all its subviews) on the specified canvas ). This view must have a complete layout before calling this function. Do not reload this method when you want to implement a view by yourself. Instead, you should reload the ondraw (canvas) method. (Note: it is mainly used for customizing view components .)
Parameters
The canvas is uploaded to the rendered view object.
Public int getinflatedid ()
Returns the ID of the loaded layout resource file. If the ID of the loaded layout resource file is no_id, the loaded view retains its original ID value.
XML attributes
Android: inflatedid
Return Value
A positive integer to identify the view to be loaded or no_id will keep the original ID of the view loaded.
See
Setinflatedid (INT)
Public int getlayoutresource ()
Returns the ID of the loaded layout resource file.
XML attributes
Android: Layout
Return Value
ID of the layout resource file loaded to the view object.
See
Setlayoutresource (INT)
Setvisibility (INT)
Inflate ()
Public View inflate ()
Load the layout resource identified by the getlayoutresource () method, and replace itself in the parent container by loading the layout resource.
Return Value
This loaded layout resource file.
Public void setinflatedid (INT inflatedid)
Set the ID of the loaded view. If this ID is no_id, the loaded view keeps its original ID unchanged.
XML attributes
Android: inflatedid
Parameters
Inflatedid is a positive integer that identifies the loaded view or no_id.
See
Getinflatedid ()
Public void setlayoutresource (INT layoutresource)
Sets the layout resource file to be loaded. It is used when viewstub is set to visible or invisible or inflate. The view created when the layout resource file is loaded is used to replace itself in the parent container.
XML attributes
Android: Layout
Parameters
Layoutresource is a valid layout resource file ID value (not equal to 0 ).
See
Getlayoutresource ()
Setvisibility (INT)
Inflate ()
Public void setoninflatelistener (viewstub. oninflatelistener inflatelistener)
Set the listener for event notification after the layout resource file is loaded successfully.
Parameters
Inflatelistener the oninflatelistener receives an event notification after it is loaded successfully.
See
Viewstub. oninflatelistener
Public void setvisibility (INT visibility)
When visibility is set to visible or invisible, inflate () is called, and viewstub is replaced in the parent container for loading view resources.
Parameters
Visibility is set to "visible", "invisible", or "gone ).
See
Inflate ()
VII. Protected Methods
Protected void dispatchdraw (canvas)
Call this function to draw the subview of the control. You can use the derived class override to obtain control before the child class is drawn (but after his own view has been drawn)
Parameters
The canvas is uploaded to the rendered view object.
Protected void onmeasure (INT widthmeasurespec, int heightmeasurespec)
Measure this view to determine the height and width of its content. This method is called Through measure (INT, INT) and should be rewritten by subclass to improve the efficiency and accuracy of content measurement.
Convention: When this method is overwritten, you must call setmeasureddimension (INT, INT) to store the height and width of the measured view. Otherwise, an illegalstateexception is thrown through measure (INT, INT. It is an effective method to call the onmeasure (INT, INT) method of the parent class.
The implementation of the parent class is based on the Default background size, unless measurespec (measurement rules) allows a larger background. To better measure the content subclass, override onmeasure (INT, INT ).
If this method is overwritten, The subclass has the responsibility to ensure that the height and width of the measurement are at least the minimum width and height of the view (getsuggestedminimumheight () and getsuggestedminimumwidth ()).
Parameters
Widthmeasurespec because the parent class has horizontal space requirements, see view. measurespec.
Heightmeasurespec because the parent class has vertical space requirements, see view. measurespec.
8. Download
Http://download.csdn.net/source/2764163 (csdn)
IX. Supplement
Article Link
Use viewstub (layout tricks: Using viewstubs)
End
Sorry. Last week, Tang Ming handed me the manuscript and asked me to help with the school draft. He was too busy to release it until now. Thanks again! Comments are also welcomed. Criticism and correction are also welcomed if the translation is inaccurate.