android: gravity center_horizontal linearlayot doesn't work

yital9 picture yital9 · Jul 10, 2012 · Viewed 11.3k times · Source

my code

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
         >

    <LinearLayout 
        android:id="@+id/foo"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:orientation="vertical"
        android:layout_gravity="center_horizontal"
        android:onClick="newItemOnclick">

        <ImageView
                        android:id="@+id/id_new_big_list_item_image"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:adjustViewBounds="true"
                        android:src="@drawable/dom_logo_new"

                   /> 


        </LinearLayout>

</LinearLayout>

but linearlayout foo align left, its not in the center. Whats wrong??

Answer

Mohsin Naeem picture Mohsin Naeem · Jul 10, 2012

try this

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal" >

<LinearLayout
    android:id="@+id/foo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="newItemOnclick"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/id_new_big_list_item_image"
        android:layout_width="50dip"
        android:layout_height="50dip"
        android:adjustViewBounds="true"
        android:src="@drawable/photo1" />
</LinearLayout>