views:

48

answers:

2

Here is some xml for a row in a ListView:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:padding="6dip">
<ImageView android:id="@+id/cover" android:layout_width="64dip"
    android:layout_height="64dip" android:layout_alignParentTop="true"
    android:layout_alignParentBottom="true" android:layout_marginRight="6dip" />
<TextView android:id="@+id/loan" style="@style/DetailsLabel.Small"
    android:layout_width="fill_parent" android:layout_height="wrap_content"
    android:layout_toRightOf="@id/cover" android:layout_alignParentRight="true" />
<TextView android:id="@+id/title" style="@style/DetailsLabel.Small"
    android:layout_width="fill_parent" android:layout_height="wrap_content"
    android:layout_toRightOf="@id/cover" android:layout_alignParentRight="true"
    android:layout_alignParentTop="true" android:layout_above="@id/loan"
    android:layout_alignWithParentIfMissing="true" android:gravity="center_vertical"
    android:singleLine="true" android:ellipsize="end" />
<RatingBar android:id="@+id/ratingBarIndicator" style="?android:attr/ratingBarStyleSmall"
    android:layout_width="fill_parent" android:layout_height="wrap_content"
    android:layout_toRightOf="@id/cover" android:layout_below="@id/title"
    android:layout_alignParentRight="true" android:gravity="center_vertical" />

And here is the result: alt text

(That's a plain Donut 1.6).

What I'm trying to achieve is a cover on the left, followed by a title, rating bar, and optional loan status on the right. As you can see the RatingBar is repeating endlessly (it should only have five stars); and whenever there IS a loan status set, the title disappears. How can I fix my XML to achieve the effect I want?

A: 

You might take a look at your layout in Heirarchy Viewer (found in the tools folder of the SDK) to get an idea of what is going wrong.

Personally, to achive that layout I would use a few LinearLayouts instead of a RelativeLayout. I find RelativeLayouts to be very confusing and hard to get right (but that is just my opinion).

For example:

<LinearLayout 
  android:orientation="horizontal" 
  android:layout_width="fill_parent" 
  android:layout_height="wrap_content">
  <ImageView
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"/>
  <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
       <TextView android:id="@+id/loan" .../>
       <TextView android:id="@+id/title" .../>
       <RatingBar android:layout_width="wrap_content .../>
  </LinearLayout>
</LinearLayout>

If you don't want the loan textView to take up any space when it is empty, you may need to set it's visibility to GONE in getView of your ListView adapter. Try it out though.

Mayra
But LinearLayouts are super bad when used as a row within a ListView. Also, I should mention that I am setting the visibility to GONE in my Java code (I'm optimistic that a loan status is set).
GJTorikian
I see you fixed your problem, but just curious why you think LinearLayouts are super bad as a row in a ListView. I've used them a lot myself, and never had any problems.
Mayra
It's better for performance, so sayeth Romain Guy: http://www.curious-creature.org/2009/02/22/android-layout-tricks-1/
GJTorikian
Interesting, thanks for the link. I wouldn't take what he says to mean "never ever use a LinearLayout" though, just that you need to keep in mind the cost. Its always a balance, code complixity vs. performance :)
Mayra
LinearLayouts are not "super bad." They are when you nest several of them. Mayra's solution is rather simple but you can probably still achieve what you want with RelativeLayout. Note that HierarchyViewer now shows how long it takes to layout/draw a screen. You can use this information to see if what you are doing has a negative impact on your performance.
Romain Guy
+1  A: 

I'd start with fixing rating bar as following:

 <RatingBar
            android:id="@+id/ratingBarIndicator"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style="?android:attr/ratingBarStyleSmall"
            android:numStars="5"
            android:isIndicator="true" />

Then align it to the right of its parent ( optionally with android:layout_centerVertical="true" ). After that position your title to the right of cover and to the left of rating bar ) And then you can have you Loaned label to be, for example, below title and to the right of cover.

Alex Volovoy
Marked as answer; though your XML cleaned up my act (somehow, I'm not actually sure how what you wrote made a difference), your paragraph explanation didn't achieve the final result I wanted. Which might have been my mistake in explaining what I wanted to get. Thanks!
GJTorikian