Android layoutinflater principle analysis, takes you step by step in-depth understanding view (a)
Source: Internet
Author: User
<span id="Label3"></p><p><p>This article was reproduced in: http://blog.csdn.net/guolin_blog/article/details/12921889</p></p><p><p>There was a period of time not to write a blog, feeling a little Rusty. The recent busy work finally came to an end, there is time to write articles, and then continue to adhere to the rhythm of each week.</p></p><p><p>A lot of my friends reacted with me, hoping that I could write an article about view, talk about how the view works, and how to customize the VIEW. yes, promised articles I must be cashed, and in view this topic I am also ready to write a few more, as far as possible to the knowledge point to speak thoroughly. So let's start with Layoutinflater today.</p></p><p><p>believe that contact with Android for a long time friends for Layoutinflater must not be unfamiliar, will know that it is mainly used to load the Layout. Friends who have just come into contact with Android may not be familiar with layoutinflater, because the task of loading the layout is usually done by calling the Setcontentview () method in the Activity. In fact Setcontentview () method of the interior is also used Layoutinflater to load the layout, but this part of the source code is internal, it is not easy to See. So today we're going to take Layoutinflater's workflow carefully and perhaps solve some of the doubts that have plagued your mind for Years.</p></p><p><p>First of all, take a look at the basic usage of layoutinflater, it is very simple to use, first need to get to the Layoutinflater instance, there are two methods can be obtained, the first one is as Follows:</p></p><pre><pre>Layoutinflater Layoutinflater = Layoutinflater.from (context);</pre></pre><p><p>Of course, there is another way to do the same thing:</p></p><pre><pre>Layoutinflater Layoutinflater =<span style="color: #000000;"> (layoutinflater) context . Getsystemservice (context.layout_inflater_service );</span></pre></pre><p><p>In fact, the first is the second simple way of writing, just Android gave us a package. Once you have an instance of layoutinflater, you can call its inflate () method to load the layout, as shown here:</p></p><pre><pre>Layoutinflater.inflate (resourceId, root);</pre></pre><p><p>The inflate () method generally receives two parameters, the first parameter is the layout ID to be loaded, and the second parameter refers to the outside of the layout and then nested a layer of parent layout, if you do not need to directly pass Null. This succeeds in successfully creating an instance of the layout and then adding it to the specified location to be Displayed.</p></p><p><p></p></p><p><p>Let's look at the usage of layoutinflater more intuitively in a very simple small example. For example, There is currently a project, where mainactivity corresponding layout file is called activity_main.xml, the code is as Follows:</p></p><pre><pre><span style="color: #0000ff;"><</span><span style="color: #800000;"></span><span style="color: #ff0000;">xmlns:android</span><span style="color: #0000ff;">= "http://schemas.android.com/apk/res/android"</span><span style="color: #ff0000;"> Android : id</span><span style="color: #0000ff;">= "@+id/main_layout"</span><span style="color: #ff0000;"> android:layout_width</span><span style="color: #0000ff;">= "match_parent"</span> <span style="color: #ff0000;"> Android:layout_height</span><span style="color: #0000ff;">= "match_parent"</span><span style="color: #0000ff;">></span><span style="color: #0000ff;"></</span><span style="color: #800000;">linearlayout</span> <span style="color: #0000ff;">></span></pre></pre><p><p>The content of this layout file is very simple, there is only an empty linearlayout, there are no controls in it, so nothing should be displayed on the Interface.</p></p><p><p></p></p><p><p>So then we'll define a layout file, name it button_layout.xml, and the code looks like This:</p></p><pre><pre><button xmlns:android= "http://schemas.android.com/apk/res/android"<span style="color: #000000;"> android:layout_width</span>= "wrap_ Content "<span style="color: #000000;"> android:layout_height</span>=" wrap_content "<span style="color: #000000;"> android:text</span>=" button "></button ></pre></pre><p><p>This layout file is also very simple, with only one Button. Now we have to figure out how to add the Button_layout layout to the linearlayout of the main layout file through Layoutinflater. Modify the code in mainactivity, as shown below, according to the usage you just introduced:</p></p><pre><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span>Mainactivity<span style="color: #0000ff;"><span style="color: #0000ff;">extends</span></span><span style="color: #000000;"><span style="color: #000000;">Activity {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span><span style="color: #000000;"><span style="color: #000000;">LinearLayout mainlayout; @Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">protected</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">onCreate (Bundle Savedinstancestate) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Super</span></span><span style="color: #000000;"><span style="color: #000000;">. OnCreate (savedinstancestate); Setcontentview (r.layout.activity_main); Mainlayout</span></span>=<span style="color: #000000;"><span style="color: #000000;">(linearlayout) Findviewbyid (r.id.main_layout); Layoutinflater Layoutinflater</span></span>= Layoutinflater.from (<span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span><span style="color: #000000;"><span style="color: #000000;">); View Buttonlayout</span></span>= Layoutinflater.inflate (r.layout.button_layout,<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">); Mainlayout.addview (buttonlayout); }}</span></span></pre><p><p>As you can see, this first obtains the instance of layoutinflater, then calls its inflate () method to load the Button_layout layout, and finally calls Linearlayout's AddView () method to add it to the Linearlayout.</p></p><p><p></p></p><p><p>You can now run the program with the results as Shown:</p></p><p><p></p></p><p><p>The button is displayed on the interface! It is true that we have successfully added the Button_layout layout to linearlayout with the help of Layoutinflater. Layoutinflater technology is widely used in the need to dynamically add view, such as in ScrollView and listview, often can see Layoutinflater figure.</p></p><p><p>of course, just the introduction of how to use Layoutinflater is clearly far from satisfying everyone's curiosity, know it is also to know its why, then we will look at the source from the perspective of how the Layoutinflater Work.</p></p><p><p>Regardless of which inflate () method you are using, you will eventually be called to Layoutinflater in the following code:</p></p><pre><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span>View Inflate (xmlpullparser parser, viewgroup root,<span style="color: #0000ff;"><span style="color: #0000ff;">Boolean</span></span><span style="color: #000000;"><span style="color: #000000;">Attachtoroot) { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">synchronized</span></span><span style="color: #000000;"><span style="color: #000000;">(mconstructorargs) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Final</span></span>AttributeSet attrs =<span style="color: #000000;"><span style="color: #000000;">Xml.asattributeset (parser); mconstructorargs[</span></span>0] =<span style="color: #000000;"><span style="color: #000000;">mcontext; View result</span></span>=<span style="color: #000000;"><span style="color: #000000;">root; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Try</span></span><span style="color: #000000;"><span style="color: #000000;"> { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">type; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> while</span>(type = Parser.next ())! = Xmlpullparser.start_tag &&<span style="color: #000000;"><span style="color: #000000;">type</span></span>!=<span style="color: #000000;"><span style="color: #000000;">Xmlpullparser.end_document) { } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(type! =<span style="color: #000000;"><span style="color: #000000;">Xmlpullparser.start_tag) { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Throw</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">inflateexception (parser.getpositiondescription ()</span></span>+ ": No start tag found!"<span style="color: #000000;"><span style="color: #000000;">); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Final</span></span>String name =<span style="color: #000000;"><span style="color: #000000;">Parser.getname (); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span><span style="color: #000000;"><span style="color: #000000;">(tag_merge.equals (name)) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(root = =<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span>|| !<span style="color: #000000;"><span style="color: #000000;">Attachtoroot) { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Throw</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>Inflateexception ("merge can is used only with a valid" + "viewgroup root and Attachtoroot=true "<span style="color: #000000;"><span style="color: #000000;">); } rinflate (parser, root, attrs); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Else</span></span><span style="color: #000000;"><span style="color: #000000;">{View Temp</span></span>=<span style="color: #000000;"><span style="color: #000000;">Createviewfromtag (name, attrs); Viewgroup.layoutparams params</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(root! =<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">) {params</span></span>=<span style="color: #000000;"><span style="color: #000000;">Root.generatelayoutparams (attrs); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(!<span style="color: #000000;"><span style="color: #000000;">Attachtoroot) {temp.setlayoutparams (params); }} rinflate (parser, temp, attrs); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(root! =<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span>&&<span style="color: #000000;"><span style="color: #000000;">Attachtoroot) {root.addview (temp, params); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(root = =<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span>|| !<span style="color: #000000;"><span style="color: #000000;">Attachtoroot) {result</span></span>=<span style="color: #000000;"><span style="color: #000000;">temp; } } } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Catch</span></span><span style="color: #000000;"><span style="color: #000000;">(xmlpullparserexception E) {inflateexception ex</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">inflateexception (e.getmessage ()); Ex.initcause (e); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Throw</span></span><span style="color: #000000;"><span style="color: #000000;">ex; } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Catch</span></span><span style="color: #000000;"><span style="color: #000000;">(ioexception E) {inflateexception ex</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">inflateexception (parser.getpositiondescription ()</span></span>+ ": " +<span style="color: #000000;"><span style="color: #000000;">E.getmessage ()); Ex.initcause (e); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Throw</span></span><span style="color: #000000;"><span style="color: #000000;">ex; } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">result; }}</span></span></pre><p><p>From here we can clearly see that layoutinflater is actually using the pull parsing method provided by Android to parse the layout file. Not familiar with the pull analytic way of friends can search online, a lot of tutorials, I do not detail, here we pay attention to the 23rd line, call the Createviewfromtag () this method, and the node name and parameters passed IN. Seeing this method name, we should be able to guess that it is used to create a view object from a node Name. indeed, within the Createviewfromtag () method, the CreateView () method is called, and the instance of the view is created and returned using Reflection.</p></p><p><p></p></p><p><p>Of course, This only creates an instance of the root layout, and then the 31st line calls the Rinflate () method to iterate through the child elements under the root layout, as shown in the following code:</p></p><pre><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>Rinflate (xmlpullparser parser, View parent,<span style="color: #0000ff;"><span style="color: #0000ff;">Final</span></span><span style="color: #000000;"><span style="color: #000000;">AttributeSet Attrs)</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">throws</span></span><span style="color: #000000;"><span style="color: #000000;">xmlpullparserexception, IOException {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Final</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>depth =<span style="color: #000000;"><span style="color: #000000;">parser.getdepth (); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">type; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> while</span>((type = Parser.next ())! = Xmlpullparser.end_tag | |<span style="color: #000000;"><span style="color: #000000;">parser.getdepth ()</span></span>> Depth) && type! =<span style="color: #000000;"><span style="color: #000000;">Xmlpullparser.end_document) { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(type! =<span style="color: #000000;"><span style="color: #000000;">Xmlpullparser.start_tag) { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Continue</span></span><span style="color: #000000;"><span style="color: #000000;">; } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Final</span></span>String name =<span style="color: #000000;"><span style="color: #000000;">Parser.getname (); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span><span style="color: #000000;"><span style="color: #000000;">(tag_request_focus.equals (name)) {parserequestfocus (parser, parent); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Else</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span><span style="color: #000000;"><span style="color: #000000;">(tag_include.equals (name)) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(parser.getdepth () = = 0<span style="color: #000000;"><span style="color: #000000;">) { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Throw</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>Inflateexception ("<include/> cannot be the root element"<span style="color: #000000;"><span style="color: #000000;">); } parseinclude (parser, parent, attrs); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Else</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span><span style="color: #000000;"><span style="color: #000000;">(tag_merge.equals (name)) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Throw</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>Inflateexception ("<merge/> must be the root element"<span style="color: #000000;"><span style="color: #000000;">); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Else</span></span><span style="color: #000000;"><span style="color: #000000;"> { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Final</span></span>View view =<span style="color: #000000;"><span style="color: #000000;">Createviewfromtag (name, attrs); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Final</span></span>ViewGroup ViewGroup =<span style="color: #000000;"><span style="color: #000000;">(viewgroup) parent; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Final</span></span>Viewgroup.layoutparams params =<span style="color: #000000;"><span style="color: #000000;">Viewgroup.generatelayoutparams (attrs); Rinflate (parser, view, attrs); Viewgroup.addview (view, params); }} parent.onfinishinflate ();}</span></span></pre><p><p>As you can see, the 21st line is also the Createviewfromtag () method to create an instance of the view, and then recursively calls the Rinflate () method on line 24th to find the child element under the view, adding the view to the parent layout after each recursive Completion.</p></p><p><p></p></p><p><p>In this way, the entire layout file is parsed to form a complete DOM structure, which will eventually return the topmost root layout, so that the inflate () process is Finished.</p></p><p><p>A more attentive friend may notice that the inflate () method also has a method overload that receives three parameters, with the following structure:</p></p><pre><pre>Inflate (<span style="color: #0000ff;">int</span><span style="color: #0000ff;">boolean</span> attachtoroot)</pre></pre><p><p>So what does this third parameter Attachtoroot mean? In fact, if you carefully read the above source code should be able to analyze the answer, here I will say the conclusion, interested friends can read the source code, Verify my conclusion is Correct.</p></p><p><p></p></p><p><p>1. If root is Null,attachtoroot will lose its function, setting any value is Meaningless.</p></p><p><p>2. If root is not set to True for null,attachtoroot, the root layout is then nested in the outermost layer of the loaded layout file.</p></p><p><p>3. If root is not set to False for null,attachtoroot, the root parameter loses Effect.</p></p><p><p>4. If the Attachtoroot parameter is not set, the default is true if root is not the Null,attachtoroot parameter.</p></p><p><p>well, now that layoutinflater's working principles and processes are clear, you should be Satisfied. The amount .... I don't think the button in this example looks a bit small, want to make it bigger? That's simple, Modify the code in the Button_layout.xml as Follows:</p></p><pre><pre><button xmlns:android= "http://schemas.android.com/apk/res/android"<span style="color: #000000;"> android:layout_width</span>= "300dp" <span style="color: #000000;"> android:layout_height</span>= "80dp"<span style="color: #000000;"> android:text</span>= "button" ></Button></pre></pre><p><p>Here we change the width of the button to 300dp, the height of 80dp, so big enough? Now run the program again to see the Effect. Hey? How the button or the original size, no changes! is the button still not big enough to change it a little bit? It's no use!</p></p><p><p></p></p><p><p>In fact, no matter how much you change the value of the Button's layout_width and layout_height, it doesn't have any effect, because these two values are now completely out of Effect. Usually we use Layout_width and layout_height to set the view size and always work as if these two properties are really used to set the view Size. In fact, they are actually used to set the view in the layout of the size, that is, the first view must exist in a layout, and then if the layout_width is set to match_parent means that the width of the view fills the layout, if set to WRAP_ Content means that the width of the view can contain its contents exactly, and if set to a specific value, the width of the view becomes the corresponding Value. This is why these two properties are called Layout_width and layout_height, not width and height.</p></p><p><p>Take a look at our button_layout.xml, it's obvious that the button is not currently present in any layout, so the Layout_width and Layout_height properties do not have any effect. So how do you change the size of the button? There are a number of workarounds, the simplest of which is to nest a layer of layout outside of the button, as shown below:</p></p><pre><pre><relativelayout xmlns:android= "http://schemas.android.com/apk/res/android"<span style="color: #000000;"> android:layout_width</span> = "match_parent"<span style="color: #000000;"> android:layout_height</span>= "match_parent" > <<span style="color: #000000;">button Android:layout_ Width</span>= "300dp"<span style="color: #000000;"> android:layout_height</span>= "80dp"<span style="color: #000000;"> android:text</span>= "button" > < /button></relativelayout></pre></pre><p><p>As you can see, here we have added a relativelayout, where the button exists and relativelayout, layout_width and Layout_height attributes are also Useful. of course, in the outermost relativelayout, its layout_width and Layout_height will lose its function. Now rerun the program and the results are as follows:</p></p><p><p></p></p><p><p>Ok! The button finally can become big, this is finally satisfied with the requirements of EVERYONE.</p></p><p><p> See here, Perhaps some friends have a great doubt in Mind. oh, no! Usually in the activity to specify the layout file, the outermost layout can be specified size ah, layout_width and layout_height are Useful. indeed, This is mainly because, in the Setcontentview () method, android automatically nests a framelayout in the outermost layer of the layout file, so the Layout_width and Layout_height properties will be Effective. So let's check it out, modify the code in Mainactivity as Follows: </p></p><pre><pre> <span style="color: #0000ff;">public </span> <span style="color: #0000ff;">class </span> mainactivity <span style="color: #0000ff;">extends </span> <span style="color: #000000;"> Activity {</span> <span style="color: #0000ff;">private </span> <span style="color: #000000;"> LinearLayout mainlayout; @Override </span> <span style="color: #0000ff;">protected </span> <span style="color: #0000ff;">void </span> <span style="color: #000000;"> OnCreate (Bundle Savedinstancestate) {</span> <span style="color: #0000ff;">super </span> <span style="color: #00000 0; ">.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); Mainlayout </span> =<span style="color: #000000;"> (linearlayout) Findviewbyid (r.id.main_layout); Viewparent viewparent </span> =<span style="color: #000000;"> mainlayout.getparent (); LOG.D (</span> "TAG", "the parent of Mainlayout is" +<span style="color: #000000;"> viewparent); }}</span></pre></pre><p><p>As you can see, the Findviewbyid () method is used to get the outermost LinearLayout object in the Activity_main layout, and then call its GetParent () method to get its parent layout and print it through log. Now rerun the program and the results are as follows:</p></p><p><p></p></p><p><p>Very correct! Linearlayout's Parent layout is really a framelayout, and this framelayout is automatically added by the system to help Us.</p></p><p><p>Speaking of which, Although the Setcontentview () method will be used by everyone, the Android interface actually shows a much more complex principle than what we see. The interface shown in any activity is essentially composed of two parts, the title bar and the content Layout. The title bar is the part that appears at the top of many interfaces, such as the one in which we have a title bar, which you can control in your code to see if it appears. And the content layout is a framelayout, the ID of this layout is called content, we call the Setcontentview () method when the layout is actually put into this framelayout, This is why this method is called Setcontentview (), not Setview ().</p></p><p><p>finally, attach a diagram of the activity window to make it easier for you to understand more intuitively:</p></p><p><p></p></p><p><p>All right, Let's talk about it today, the support, the spit, the doubt, and the soy Sauce's passing friends. ^v^ interested friends can continue to read the Android view drawing process to fully parse, take you step by step in-depth understanding of view (ii).</p></p><p><p>Android layoutinflater principle analysis, takes you step by step in-depth understanding view (a)</p></p></span>
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