Ika
Ika

Reputation: 1556

Can't Align ImageView in RelativeLayout

I have a layout containing two textViews and one ImageView.

the first text is aligned to the left. The second is also aligned to the left and is below the first text. the image is aligned to the RIGHT! and is aligned to the top of the first text and the bottom of the second text.

I tried to use a RelativeLayout for the job but the Image refuse to align to the right.

Here is my xml

<RelativeLayout
            android:id="@+id/NameAndPhoto"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >
            <LinearLayout
                android:id="@+id/Names"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical" >

                <TextView
                    android:id="@+id/FirstName"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="@style/contact_page_name_text_style" >
                </TextView>

                <TextView
                    android:id="@+id/LastName"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="@style/contact_page_name_text_style" >
                </TextView>
            </LinearLayout>

            <ImageView
                android:id="@+id/PersonPhoto"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignBottom="@id/Names"
                android:layout_alignParentRight="true"
                android:layout_alignTop="@id/Names"
                android:layout_toRightOf="@id/Names"
                android:contentDescription="@string/contact_page_photo_content_description"
                android:padding="3sp"
                android:scaleType="fitCenter" >
            </ImageView>
        </RelativeLayout>

Please advise.

Thanks, Ika.

Upvotes: 2

Views: 8085

Answers (2)

bwoogie
bwoogie

Reputation: 4427

I'm not exactly sure what you're looking for... And I'm not sure if it's good practice to nest a ton of LienarLayouts... But here is something that might help?

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <LinearLayout
        android:id="@+id/Names"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:orientation="vertical" android:layout_weight="1">

        <TextView
            android:id="@+id/FirstName"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="first name" />

        <TextView
            android:id="@+id/LastName"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="last name" />
    </LinearLayout>

    <ImageView
        android:id="@+id/PersonPhoto"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="3sp"
        android:scaleType="fitCenter"
        android:src="@drawable/ic_launcher" android:layout_weight="1"/>

</LinearLayout>

Upvotes: 0

Gangnus
Gangnus

Reputation: 24464

If you wish to use LinearLayout, use width="match_parent" and align by setting gravity="right". So you move text in the view that has the width of the parent.

As for your placing ImageView in RelativeLayout, you have a contradiction there: android:layout_alignParentRight="true" and android:layout_toRightOf="@id/Names" - two different horizontal placings.

Also, you can't use simultaneously bottom and top placing. And you are using them in Image. Again a contradiction.

And how can you place the Image to the right of the LinearLayout that takes all the width of the root layout? Where could it be placed? Change width of the Names to wrap_content

Upvotes: 5

Related Questions