<span id="Label3"></p>It is still very useful to layoutinflater this class in Real-world development, which acts like Findviewbyid (). The difference is that Layoutinflater is used to find the XML layout file under Res/layout/and is instantiated, while Findviewbyid () is looking for specific widget controls (such as button, textview, and so on) under the XML layout file. Specific role: 1, for a non-loaded or want to dynamically load the interface, you need to use Layoutinflater.inflate () to load, 2, for an already loaded interface, you can use Activiyt.findviewbyid () method to obtain the interface Elements. Layoutinflater is an abstract class that is declared in the document as Follows: public abstract class Layoutinflater extends object three ways to get a Layoutinflater instance<span style="color: #ff0000;"><span style="color: #ff0000;">1. Layoutinflater inflater = getlayoutinflater ();//invoke activity's getlayoutinflater ()</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">2. Layoutinflater inflater = Layoutinflater.from (context); </span></span><span style="color: #ff0000;"><span style="color: #ff0000;">3. Layoutinflater inflater = (layoutinflater) context.getsystemservice (context.layout_inflater_service);</span></span>In fact, these three ways are essentially the same, from the source can be seen: getlayoutinflater (): Activity of the Getlayoutinflater () method is called Phonewindow Getlayoutinflater () method , take a look at the source code: public Phonewindow (context Context) {super (context); Mlayoutinflater = Layoutinflater.from (context);} It can be seen that it is actually called Layoutinflater.from (context). Layoutinflater.from (context): public static layoutinflater from (context context) {layoutinflater layoutinflater = (layo Utinflater) Context.getsystemservice (context.layout_inflater_service); if (layoutinflater = = Null) {throw new Assertionerror ("layoutinflater not found."); } return layoutinflater;} It can be seen that it actually calls Context.getsystemservice ().<span style="color: #ff0000;"><span style="color: #ff0000;">There are three methods of Layoutinflater.inflate overloading:</span></span> <span class="keyword">public <span class="keyword">View Inflate (</span></span><span class="keyword"><span class="keyword"><span class="keyword">int resource, ViewGroup root)</span></span></span><span style="color: #ff0000;"><span style="color: #ff0000;"><span class="keyword"><span class="keyword"> <span class="keyword"><span class="keyword"><span class="keyword"> </span></span></span></span></span></span></span><span class="keyword">public <span class="keyword">View Inflate (<span class="keyword">int resource, viewgroup root, <span class="keyword">boolean attachtoroot);<br></span></span></span></span><br><p><p>Analytical:</p></p><p><p>If we viewgroup root is set to null, we will return directly to the view we created; layoutparams (default parent View)</p></p><p><p>If the ViewGroup root parameter is set and Attachtoroot is set to true, the view we load is added to the root view as a child view.</p></p><p><p>If root is not empty and attachtoroot is set to false, return to the above Paragraph: set the Layoutparams for the view that we loaded. (of its Own)</p></p><p><p><br></p></p><p><p>Android's Layoutinflater Detailed</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