--layoutinflater of system services provided by Android (layout service)
--Reprint Please specify source: Coder-pig
Introduction to this section:
In this section, we are simply introducing thelayoutinflater (layout service)of the Android system service.
I believe we are accustomed to using the Activity.setcontentview () method to our layout file,
The bottom layer is still done by the layoutinflater of this system!
The principle of operation is to use Android built-in pull parser to parse layouts ~
and Blog written in the previous days used in the dynamic layout, is to use this stuff, interested can see
Plain Java code load layout: http://blog.csdn.net/coder_pig/article/details/43983709
Java code dynamically adding components or layouts: http://blog.csdn.net/coder_pig/article/details/43988511
In fact, the basic usage example is in the above two blog, this section is some concept of nagging ~
The text of this section:
What the hell is 1.LayoutInflater?
A: A system service for loading layouts! is to instantiate the view object corresponding to the layout XML file, which cannot be used directly,
need to pass Getlayoutinflater () method or Getsystemservice () method to obtain the current context bound
Layoutinflater instances!
usage of 2.LayoutInflater:① three ways to get Layoutinflater:
Method one Layoutinflater Inflater1 = Layoutinflater.from (this);//Method two Layoutinflater Inflater2 = Getlayoutinflater ();// Method three Layoutinflater Inflater3 = (layoutinflater) getsystemservice (Layout_inflater_service);
PS: In fact, the first two methods of the underlying code are called the third thing ~
② How to load layouts:
Public View Inflate (int resource, ViewGroup root, Boolean attachtoroot)
The three parameters of this method are:
① the layout ID to load
② a layer of parent layout for the outside of the layout, and if not, write null if you want!
③ whether to load the outermost layer of the layout file with the root layout, without setting this argument, if
If root is not NULL, the default is true, and if Root is null, Attachtoroot will not work!
Root is not true for Null,attachtoroot, the root layout is nested in the outermost layer of the loaded layout file;
If it is false, Root loses its function!
Simply understand: add a root outer container for the loaded layout!
③ to set the related properties by Layoutinflater.layoutparams:
For example, Relativelayout can also be added by the addRule method, that is, set the location, is the reference to the parent container,
Refer to the child control, or set margin, etc., this is your decision ~
Let's write a simple example here:
Activity_main.xml:
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " android:layout_width=" match_parent " android:layout_height=" Match_parent " tools:context= "com.example.test.MainActivity" android:id= "@+id/rly" > <button android:id = "@+id/btnone" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android: text= "I am setcontentview () loaded button"/></relativelayout>
Mainactivity.java:
package Com.example.test;import Android.app.activity;import Android.os.bundle;import Android.view.layoutinflater;import Android.widget.button;import Android.widget.relativelayout;import Android.widget.RelativeLayout.LayoutParams; public class Mainactivity extends Activity {@Overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Button Btntwo = New button (this); Btntwo.settext ("I am a dynamically loaded button"); Relativelayout rly = (relativelayout) Findviewbyid (r.id.rly); Layoutinflater Inflater = (layoutinflater) getsystemservice (Layout_inflater_service); Relativelayout.layoutparams LP = new Layoutparams (RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT); Lp.addrule (Relativelayout.below,r.id.btnone); Lp.setmargins (10, 10, 10, //addview () method can set different parameters Oh ~rly.addview (BTNTWO, LP);}}
Run:
Code is relatively simple, do not explain the ~, and finally provide the next layoutinflater of the inflate () method of the source code,
Interested can be studied under:
Public View Inflate (Xmlpullparser parser, ViewGroup root, Boolean attachtoroot) {synchronized (Mconstructorargs) { Final AttributeSet attrs = xml.asattributeset (parser); Mconstructorargs[0] = Mcontext; View result = root; try {int type; while (type = Parser.next ())! = Xmlpullparser.start_tag && Type! = Xmlpullparser.end_documen T) {} if (type! = Xmlpullparser.start_tag) {throw new Inflateexception (parser . Getpositiondescription () + ": No start tag found!"); } final String name = Parser.getname (); if (tag_merge.equals (name)) {if (root = null | |!attachtoroot) {throw new inflate Exception ("Merge can is used only with a valid" + "ViewGroup root and Attachtoroot=true"); } rinflate (ParsEr, root, attrs); } else {View temp = Createviewfromtag (name, attrs); Viewgroup.layoutparams params = null; if (root = null) {params = Root.generatelayoutparams (attrs); if (!attachtoroot) {temp.setlayoutparams (params); }} rinflate (parser, temp, attrs); if (root = null && attachtoroot) {Root.addview (temp, params); } if (root = null | |!attachtoroot) {result = temp; }}} catch (Xmlpullparserexception e) {inflateexception ex = new inflateexception (e.g Etmessage ()); Ex.initcause (e); Throw ex; } catch (IOException e) {inflateexception ex = new Inflateexception (parser.getpositionDescription () + ":" + e.getmessage ()); Ex.initcause (e); Throw ex; } return result; } }
Well, this section is here, if you have anything to add, Welcome to ~o (∩_∩) O Thank you ~
Resources:
Guo Dashu's blog:http://blog.csdn.net/guolin_blog/article/details/12921889
--layoutinflater of system services provided by Android (layout service)