"Go" Android API Chinese (--viewstub)

Source: Internet
Author: User
Tags ming

Fills the view with an XML source.
Inflate (Context object, resource file ID, parent window group is generally null);

Original URL: http://www.cnblogs.com/over140/archive/2010/10/18/1853989.html

Objective

Keywords: android.view.ViewStub, version for Android 2.2 R1

This chapter translation from Tong Ming , here is responsible for organizing and publishing, welcome other translators to participate in the Android API's Chinese translation action, contact me [email protected].

Statement

Welcome reprint, but please keep the original source of the article:)

Blog Park: http://www.cnblogs.com

Farmer Uncle: http://www.cnblogs.com/over140/

Body

First, the structure

    Public final class Viewstub extends View

    

Java.lang.Object

Android.view.View

Android.view.ViewStub

Ii. Overview of the class

Viewstub is a hidden, non-memory-intensive view object that can delay loading a layout resource file at run time. This layout resource file is loaded when viewstub is visible, or when the inflate () function is called. The viewstub replaces itself in the parent container when the view is loaded. Therefore, viewstub is always present in the view until setvisibility (int) or inflate () is called. The layout parameters of the viewstub are added to the Viewstub parent container along with the number of views loaded. Similarly, you can also define or rename the ID value of the View object to be loaded by using the Inflatedid property. For example:

    

The defined Viewstub object can be found through the "Stud" ID. After loading the layout resource file "Mysubtree", the Viewstub object is removed from its parent container. The view created by the layout resource "Mysubtree" can be found through the ID "subtree". This view object is finally assigned a width of 120dip and a height of 40dip. The recommended way to load a layout resource file is as follows:

    

When inflate () is called, the viewstub loaded view overrides and returns the View object. This allows the application to get a reference to the load view without additional execution of Findviewbyid ().
(Translator Note: This class is probably meant to be associated with the Viewstub class and the layout resource file specified in the XML file, so that the layout resource file is loaded when it needs to be used.) The main role is performance optimization, when to use when loading, not at the start of the time when the load, can speed up the program startup speed, but also save memory resources. )

Three, nested class

Interface Viewstub.oninflatelistener a listener that receives notification that VIEWSTUB has successfully loaded the layout resource file.

Iv. XML attributes

Property name

Related methods

Describe

Android:inflatedid

Setinflatedid (int)

Overwrites the ID value of the view to be loaded.

Android:layout

Setlayoutresource (int)

Provides an identity for the resource view to be loaded, which is used when viewstub becomes visible or gets focus. (Translator Note: The layout resource file ID to be referenced)

Five, the constructor function

Viewstub (context context, int layoutresource)

Creates a Viewstub object that is associated with the specified layout resource file.

Parameters

Layoutresource the ID value of the layout resource file to load.

Vi. Public methods

public void Draw (canvas canvas)

Draws this view (and all its sub-views) manually on the specified canvas. This view must be in the overall layout before calling this function. Do not overload this method when you want to implement a view yourself, instead, you should reload the OnDraw (Canvas) method. (Translator Note: Methods that are primarily used for custom view components.) )

Parameters

Canvas This canvas is passed to the rendered view object.

public int Getinflatedid ()

Returns the ID of the loaded layout resource file, and if the ID of the loaded layout resource file is no_id, then the loaded view retains its original ID value.

Related XML properties

Android:inflatedid

return value

A positive integer to identify the view to load or the no_id will keep the original ID of the loaded view.

See

Setinflatedid (int)

public int Getlayoutresource ()

Returns the ID value of the loaded layout resource file.

Related XML properties

Android:layout

return value

The ID value of the layout resource file that is loaded into the View object.

See

Setlayoutresource (int)

setvisibility (int)

Inflate ()

Public View Inflate ()

The load Getlayoutresource () method identifies the layout resource and replaces it itself in the parent container by loading the layout resource.

return value

This loaded layout resource file.

public void Setinflatedid (int inflatedid)

Sets the ID of the load view. If this ID is no_id, this load view keeps its original ID intact.

Related XML properties

Android:inflatedid

Parameters

Inflatedid a positive integer to identify this loaded view or no_id will keep the original ID of the loaded view.

See

Getinflatedid ()

public void Setlayoutresource (int layoutresource)

Sets the layout resource file to be loaded when viewstub is set to visible or invisible or called inflate (). This view, created when loading a layout resource file, is used to replace itself in the parent container.

Related XML properties

Android:layout

Parameters

Layoutresource a valid layout resource file ID value (not equal to 0).

See

Getlayoutresource ()

setvisibility (int)

Inflate ()

public void Setoninflatelistener (Viewstub.oninflatelistener inflatelistener)

Sets the listener for event notification after the layout resource file has been successfully loaded.

Parameters

Inflatelistener the Oninflatelistener gets an event notification after a successful load.

See

Viewstub.oninflatelistener

public void setvisibility (int visibility)

When visibility is set to visible or invisible,inflate () is called, and the Load view resource replaces viewstub in the parent container.

Parameters

Visibility is set to visible (display), INVISIBLE (hidden), or GONE (completely hidden, not take up layout position).

See

Inflate ()

Vii. protected methods

protected void Dispatchdraw (canvas canvas)

Call this function to draw a child view of the control. Can be overridden by a derived class to get control before drawing a subclass (but after his own view has been drawn)

Parameters

Canvas This canvas is passed to the rendered view object.

protected void onmeasure (int widthmeasurespec, int heightmeasurespec)

Measure this view to determine the height and width of its contents. This method is called by measure (int, int) and should be overridden by subclasses to improve the efficiency and accuracy of content measurements.

Convention: When the method is overridden, you must call setmeasureddimension (int, int) to store the height and width of the measured view. Otherwise, a IllegalStateException exception will be thrown through measure (int, int). Calling the onmeasure (int, int) method of the parent class is an effective way.

The implementation of the parent class is based on the default size of the background size, unless the measurespec (measure rule) allows for a larger background. To better measure the content subclasses should override onmeasure (int, int).

If this method is overridden, the subclass is responsible for ensuring that the height and width of the measurement is at least the minimum width and height of the view (Getsuggestedminimumheight () and Getsuggestedminimumwidth ()).

Parameters

Widthmeasurespec because the parent class has lateral space requirements, see View.measurespec.

Heightmeasurespec because the parent class has vertical space requirements, see View.measurespec.

Eight, download

csdn:http://download.csdn.net/source/2764163

Ix. Supplementary

Article links

"Translation" Using Viewstub (Layout tricks:using viewstubs)

End

More sorry, last week Tong Ming has handed me the manuscript, and told me to help the school draft, because their side of the relatively busy has been delayed until now released, thank him again! Also welcome comments, translation inaccuracies in the place also welcome criticism.

Category: 4, Android Chinese API

"Go" Android API Chinese (--viewstub)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.