Inflating TextView and LinearLayout programmatically
<p>The problem here I don't get the same output view result in these two cases, I want to fix case 1 to get same output result as case 2:</p>

<ol>
<li>Inflate textview and linearlayout, and then add textview to linearlayout programmatically.</li>
<li>Add textview to linearlayout in xml.</li>
</ol>

<p><strong>Code and Output For Case 1:</strong></p>

<p>main.xml</p>

<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"&gt;
&lt;/LinearLayout&gt;
</code></pre>

<p>text_view.xml</p>

<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:textColor="#000000"
    android:text="1"
    android:textSize="20sp"
    android:background="#AAAAAA"
    android:gravity="center"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_marginTop="2dp"
    android:layout_marginRight="2dp"
 /&gt;
</code></pre>

<p>onCreate method in LayoutTestActivity.java</p>

<pre><code>public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    LinearLayout lt = (LinearLayout) getLayoutInflater().inflate(R.layout.main, null);
    TextView tv = (TextView) getLayoutInflater().inflate(R.layout.text_view, null);
    lt.addView(tv);
    setContentView(lt);
}
</code></pre>

<p>output view (Not Correct)</p>

<p><img src="http://i.stack.imgur.com/sWnsQ.png" alt="enter image description here"></p>

<p><strong>Code and Output For Case 2:</strong></p>

<p>main.xml</p>

<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"&gt;
    &lt;TextView
        android:textColor="#000000"
        android:text="1"
        android:textSize="20sp"
        android:background="#AAAAAA"
        android:gravity="center"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginTop="2dp"
        android:layout_marginRight="2dp"
     /&gt;
&lt;/LinearLayout&gt;
</code></pre>

<p>onCreate method in LayoutTestActivity.java</p>

<pre><code>public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}
</code></pre>

<p>output view (Correct)</p>

<p><img src="http://i.stack.imgur.com/PyB46.png" alt="enter image description here"></p>
