Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GridView - sometimes rows are top-aligned, sometimes they're bottom-aligned!

Tags:

android

I am using a GridView. Four items in a column. Each element is composed of the following layout:

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:orientation="vertical">

  <ImageView
    android:layout_width="32dip"
    android:layout_height="32dip"/>

  <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="11dip"
    android:paddingTop="2dip" />
</LinearLayout>

So each element is a small image with a bit of text below it. The layout works great on the first pass. However, if an item's text is long, it wraps to two lines. This works ok, until you scroll it in and out of view. By default, all the images in a row are top-aligned. When you scroll a row off-screen, then back on screen, you'll see that the row somehow gets bottom-aligned. It looks like:

First layout:

[image]   [image]   [image]  
[text]    [text]    [text
                     wrap]

so even though the third element has 2 lines of text, the tops are aligned which is perfect. If I scroll this row off screen, then back on, it looks like this:

                    [image]
[image]   [image]   [text
[text]    [text]     wrap]

so the row gets bottom-aligned here. I'm not sure if this is a bug in GridView, or if there's some way to control the layout to always top-align rows. I've tried setting the gravity of the element layout to "top", no good. There also doesn't appear to be any setting unique to GridView to control this. Any ideas?

Thanks

like image 368
user291701 Avatar asked Nov 05 '10 12:11

user291701


1 Answers

The height of the items in the GridView must be the same. You have 2 or 3 options.

  1. Give a height to the TextView
  2. Make the TextView single line with android:singleLine="true"
  3. If you must have 2 lines in some of the items, make all the items have 2 lines. You can do that android:minLines="2"
like image 167
tasomaniac Avatar answered Nov 09 '22 21:11

tasomaniac