android margins not working when dynamically inserting views : <p>I have a simple view:</p>

<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/contact_selected"
    android:layout_marginTop="10dp"
    android:layout_marginEnd="5dp"
    android:orientation="horizontal"
    android:padding="3dp"&gt;
    &lt;TextView
        android:id="@+id/txt_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Billy Bob"
        /&gt;
&lt;/LinearLayout&gt;
</code></pre>

<p>When I statically copy the LinearLayout markup into my main activity layout, the margins are as expected.  However, when I add the view into the main activity layout dynamically, the margins are ignored.  Here's how I insert the view</p>

<pre><code>LayoutInflater inflater = (LayoutInflater)
        getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.test, null);
TextView txt_title = (TextView)view.findViewById(R.id.txt_title);
txt_title.setText("Dynamic #1");
llayout.addView(view, llayout.getChildCount()-1);

View view2 = inflater.inflate(R.layout.test, null);
txt_title = (TextView)view2.findViewById(R.id.txt_title);
txt_title.setText("Dynamic #2");
llayout.addView(view2, llayout.getChildCount()-1);
</code></pre>

<p>Here's what it looks like:</p>

<p><a href="https://i.stack.imgur.com/7si8I.png" rel="noreferrer"><img src="https://i.stack.imgur.com/7si8I.png" alt="enter image description here"></a></p>

<p>The container in the main layout is a LinearLayout, which is a child of a HorizontalScrollView.  Any insight is appreciated.</p>
