Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I've investigated this issue, referring to the <a href="http://developer.android.com/reference/android/view/LayoutInflater.html">LayoutInflater docs</a> and setting up a small sample demonstration project. The following tutorials shows how to dynamically populate a layout using <code>LayoutInflater</code>.</p> <p>Before we get started see what <code>LayoutInflater.inflate()</code> parameters look like:</p> <ul> <li><strong>resource</strong>: ID for an XML layout resource to load (e.g., <code>R.layout.main_page</code>)</li> <li><strong>root</strong>: Optional view to be the parent of the generated hierarchy (if <code>attachToRoot</code> is <code>true</code>), or else simply an object that provides a set of <code>LayoutParams</code> values for root of the returned hierarchy (if <code>attachToRoot</code> is <code>false</code>.)</li> <li><p><strong>attachToRoot</strong>: Whether the inflated hierarchy should be attached to the root parameter? If false, root is only used to create the correct subclass of <code>LayoutParams</code> for the root view in the XML.</p></li> <li><p><strong>Returns</strong>: The root View of the inflated hierarchy. If root was supplied and <code>attachToRoot</code> is <code>true</code>, this is root; otherwise it is the root of the inflated XML file. </p></li> </ul> <p>Now for the sample layout and code.</p> <p>Main layout (<code>main.xml</code>):</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent"&gt; &lt;/LinearLayout&gt; </code></pre> <p>Added into this container is a separate TextView, visible as small red square if layout parameters are successfully applied from XML (<code>red.xml</code>):</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="25dp" android:layout_height="25dp" android:background="#ff0000" android:text="red" /&gt; </code></pre> <p>Now <code>LayoutInflater</code> is used with several variations of call parameters</p> <pre><code>public class InflaterTest extends Activity { private View view; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ViewGroup parent = (ViewGroup) findViewById(R.id.container); // result: layout_height=wrap_content layout_width=match_parent view = LayoutInflater.from(this).inflate(R.layout.red, null); parent.addView(view); // result: layout_height=100 layout_width=100 view = LayoutInflater.from(this).inflate(R.layout.red, null); parent.addView(view, 100, 100); // result: layout_height=25dp layout_width=25dp // view=textView due to attachRoot=false view = LayoutInflater.from(this).inflate(R.layout.red, parent, false); parent.addView(view); // result: layout_height=25dp layout_width=25dp // parent.addView not necessary as this is already done by attachRoot=true // view=root due to parent supplied as hierarchy root and attachRoot=true view = LayoutInflater.from(this).inflate(R.layout.red, parent, true); } } </code></pre> <p>The actual results of the parameter variations are documented in the code.</p> <p><strong>SYNOPSIS:</strong> Calling <code>LayoutInflater</code> without specifying root leads to inflate call ignoring the layout parameters from the XML. Calling inflate with root not equal <code>null</code> and <code>attachRoot=true</code> does load the layout parameters, but returns the root object again, which prevents further layout changes to the loaded object (unless you can find it using <code>findViewById()</code>). The calling convention you most likely would like to use is therefore this one:</p> <pre><code>loadedView = LayoutInflater.from(context) .inflate(R.layout.layout_to_load, parent, false); </code></pre> <p>To help with layout issues, the <a href="https://developer.android.com/studio/profile/hierarchy-viewer.html">hierarchy viewer</a> is highly recommended.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload