tags:

views:

141

answers:

2

Im trying to define a LinearLayout which contains another LinearLayout which should always be desplayed in the horizontal and vertical center. An ImageView should be placed always vertically centered on the right side of the parent Layout:

                                 A                                           B

I defined it the following:

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="60px">

     <LinearLayout android:id="@+id/footer" android:orientation="horizontal" android:layout_height="50px" android:paddingTop="5px" android:layout_width="wrap_content" android:background="@drawable/footer_border" android:paddingRight="5px" android:paddingLeft="5px" android:layout_gravity="center_horizontal">
        <ImageButton android:id="@+id/libraryButton" android:layout_height="wrap_content" android:src="@drawable/library_button" android:background="#000000" android:layout_width="wrap_content"/>
        <ImageButton android:id="@+id/bookButton" android:layout_height="wrap_content" android:src="@drawable/book_button" android:background="#000000" android:layout_width="wrap_content" android:paddingLeft="15px" android:paddingRight="15px"/>
        <ImageButton android:id="@+id/workspaceButton" android:layout_height="wrap_content" android:src="@drawable/workspace_button" android:background="#000000" android:layout_width="wrap_content"/>
     </LinearLayout>

     <ImageView
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_gravity="right"
     android:id="@+id/loading">
     </ImageView>

     </LinearLayout> 

But unfornatuley its not working... The LinearLayout (A) and the ImageView (B) is on the left side.... But i set gravity to center and right?? Why?

A: 

Try setting the android:layout_alignParentRight parameter

<ImageView
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_gravity="right"
 android:id="@+id/loading"
 android:layout_alignParentRight="true">
</ImageView>

Tell me if that works...

Cristian
unfortunately it doesn't work. still all layout views on the left side
Roflcoptr
Sigh... cannot test it right now :( Sorry.
Cristian
np ;) maybe someone else knows an answer ;)
Roflcoptr
+1  A: 

Gravity in LinearLayout with horizontal orientation will only work for top, bottom and center_vertical value(here).

I think the best way to achieve such a thing is to use a RelativeLayout instead of LinearLayout.Something like:

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="60px">

<LinearLayout android:id="@+id/footer" 
    android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:center_in_parent="true"
>
....
</LinearLayout>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/loading"
android:align_parent_right="true">
</ImageView>

</RelativeLayout> 

I'm sorry but i can't test it right now..I hope it is right..

hara
I tested your layout but there are to errors:ERROR No resource identifier found for attribute 'align_parent_right' in package 'androidandERROR No resource identifier found for attribute 'center_in_parent' in package 'android'
Roflcoptr
ah ok i found the syntax error in your code. i test it again.
Roflcoptr
thanks a lot your solution works. i addedandroid:layout_centerVertical="true"to the ImageView and now its perfectBut this is really another nasty bug in android which costs some hours to detect like the ArrayList Bug in Android 1.5
Roflcoptr
You are welcome :)
hara