Ignacio Garat
Ignacio Garat

Reputation: 1556

How to position images witihin a LinearLayout?

I have the following custom view within a gallery works fine. I would like the previous/next imageviews to float left/right. (the previous image next to the left margin and viceversa with right image) Any ideas?

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

<ImageView
    android:id="@+id/gallery_left"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="left"
    android:layout_marginLeft="0dp"
    android:src="@drawable/previous" />

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/fragment_gallery_item_imageview"
        android:layout_width="40dp"
        android:layout_height="40dp" />

    <TextView
        android:id="@+id/fragment_gallery_item_textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="New Text"
        android:textSize="20sp" />
</LinearLayout>

<ImageView
    android:id="@+id/gallery_right"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="right"
    android:layout_marginRight="0dp"
    android:src="@drawable/next" />


</LinearLayout>

Thank you very much.

Upvotes: 0

Views: 539

Answers (1)

Shooky
Shooky

Reputation: 1299

Easiest way would be to change your top level LinearLayout to a RelativeLayout. Set the left image

alignParentLeft="true"

Center image/container:

layout_centerHorizontal="true"

and Right image:

alignParentRight="true"

Upvotes: 1

Related Questions