Difference between setContentView and inflate, setcontentview
Difference between setContentView and inflate
Generally, LayoutInflater is used to do one thing: inflate
The inflate method has a total of four forms (see the following). The purpose is to convert the layout expressed in xml into a View object.
One of them is commonly used: View inflate (int resource, ViewGroup root), and the other three are similar in purpose.
Int resource, that is, the ID of the resource/layout file in the R file, which must be specified.
ViewGroup root can be null. If it is null, only the View corresponding to the resource is created. If it is not null, the created view is automatically added as the root child.
Difference between setContentView and inflate:
Once setContentView () is called, layout will immediately display the UI. inflate will only form a ready-made object in the view class and display it with setContentView (view) when necessary.
Generally, the interface is displayed through setContentView () in the activity. However, if you want to set the control layout in a non-activity, LayoutInflater needs to be dynamically loaded.
<TextView
Android: id = "@ + id/tview"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "ATAAW. COM"
/>
<Button
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: id = "@ + id/button"
Android: text = "button"
/>
Dynamically load the above layout in the program.
LayoutInflater flater = LayoutInflater. from (this );
View view = flater. inflate (R. layout. example, null );
Obtain the control in the layout.
Button = (Button) view. findViewById (R. id. button );
TextView = (TextView) view. findViewById (R. id. tview );
**************************************** *******************
Next, let's talk about the four forms of the inflate method based on the source code:
The inflate method has four forms in total. layout expressed in xml is converted to view. this class is used to instantiate layout xml files into its corresponding view object. it is never be used directly -- use getLayoutInflater () or getSystemService (String) getLayoutInflate () or getSystemService (String) to retrieve a standard LayoutInflater instance that is already hooked up that is already hook up to the current context and correct configured for the device you are running on.
1. Context. public abstract object getSystemService (String name)
2. Two methods for obtaining LayoutInflater
A. obtained through SystemService
LayoutInflater inflater = (LayoutInflater) context. getSystemService (Context. LAYOUT_INFLEATER_SERVICE );
B. obtain from the given context
Public static LayoutInflater from (Context context)
C. The difference between the two: in fact, the same, source code
/**
* Obtains the LayoutInflater from the given context.
*/
Public static LayoutInflater from (Context context ){
LayoutInflater =
(LayoutInflater) context. getSystemService (Context. LAYOUT_INFLATER_SERVICE );
If (LayoutInflater = null ){
Throw new AssertionError ("LayoutInflater not found .");
}
Return LayoutInflater;
}
3. LayoutInflater. inflate () converts the Layout file to View, which is used exclusively for Layout. Although Layout is also a subclass of View, if you want to convert Layout in xml to View in. java code in android, you can only use Inflater instead of findViewById ().
4. LinearLayout linearLayout =
(LinearLayout) findViewById (R. id. placeslist_linearlayout );
LinearLayout. addView (place_type_text );
5. Two findViewById formats are available:
R. layout. xx references res/layout/xx. xml layout file (inflate method), R. id. xx refers to the component in the layout file, and the component id is xx (findViewById method ). All component IDS can be viewed using R. id. xx, but the component cannot be used without layout in setContentView (). The Activity. findViewById () will encounter a null pointer exception.
A. findViewById (int id) in activity)
B. findViewById (int id) in View)
6. The difference is that LayoutInflater is used to find the xml layout file under layout and instantiate it! FindViewById () is used to find the widget controls (such as buttons and TextView) in a specific xml file ).
What is the difference between inflate and expand?
Inflate refers to inflation. inflate objects are expanded objects. Passive usage: xxx is inflated or inflate xxx.
Expand refers to the increase of size and volume or the enhancement of importance. expand is the action of an object. Active usage: xxx expands
You can use expand to describe the inflate action, for example, the following sentence:
Inflate a tyre to blow the tires.
A tyre expands when you pump air into it. when the tire gets angry, it gets up.
What is the difference between using the Intent call interface and using setContentView?
Jump to the activity, which is to enter another activity and display its page setcontentview. The current activity has not changed, but the view has changed.