How to center an ImageView in a LinearLayout?
<p>The following XML code doesn't work to center an ImageView in a LinearLayout:</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="fill_parent"
    android:layout_height="wrap_content"&gt;
    &lt;ImageView
        android:id="@+id/myimg" 
        android:layout_width="36dip"
        android:layout_height="36dip"
        android:scaleType="fitXY"
        android:layout_gravity="center_horizontal"
        android:background="@drawable/my_background"
    /&gt;
&lt;/LinearLayout&gt;
</code></pre>

<p>And this is RelativeLayout code which surprisingly works:</p>

<pre><code> &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"&gt;
    &lt;ImageView
        android:id="@+id/myimg" 
        android:layout_width="36dip"
        android:layout_height="36dip"
        android:scaleType="fitXY"
        android:layout_centerHorizontal="true"
        android:background="@drawable/my_background"
    /&gt;
&lt;/RelativeLayout&gt;
</code></pre>

<p>Can someone tell me why? Thanks!</p>
