EDIT after comments:
It turns out that doing this work with RelativeLayout is not easy. At the bottom of the answer, I turned on RelativeLayout, which gives the desired effect, but only until it is included in the ListView. After that, the same problems arose as in the question. This has been fixed using LinearLayout (s).
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="10dp" android:orientation="horizontal"> <ImageView android:id="@+id/pickImageImage" android:layout_width="100dp" android:layout_height="80dp" android:background="@drawable/icon" android:scaleType="fitXY" android:layout_marginRight="10dp"/> <TextView android:id="@+id/pickImageText" android:layout_height="fill_parent" android:layout_width="fill_parent" android:gravity="left|center_vertical" android:text="I'm the text"/> </LinearLayout>
If you want to have two text fields, you can nest the second orientation="vertical" and LinearLayout after the ImageView, and then put the text fields there.
This works, but I must admit that I do not know why RelativeLayouts did not. For example, this blog post by Romain Guy suggests that RelativeLayout should. When I tried this, I never had enough labor; admittedly, I did not do this exactly like him, but my only changes were related to some TextViews attributes that should not have made that much difference.
Here's the original answer:
I think you're confusing Android with all of these somewhat conflicting instructions in RelativeLayout. I reformatted your case:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/row" android:layout_width="fill_parent" android:layout_height="wrap_content" android:padding="10dip"> <ImageView android:id="@+id/item_image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingRight="10dip" android:src="@drawable/icon" android:layout_alignParentLeft="true" android:layout_alignParentTop="true"/> <TextView android:id="@+id/item_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/item_image" android:layout_centerVertical="true" android:text="Blah!"/> </RelativeLayout>
And it works great. I removed many of your redundant android:layout_alignParentxxx because they were not needed. This view now displays the image in the upper left corner and the text vertically next to it. If you want the image to be also vertically centered, you cannot have a RelativeLayout on android: layout_height = "wrap_content" because it is trying to make itself no higher than the height of the image. You must indicate the height, for example. 80dp, and then set the ImageView to a fixed height, like 60dp, with android: scaleType = "fitXY" to zoom out to fit.
Steve haley
source share