GridLayout Child View Clipping Issues
After back-porting GridLayout to 1.5+, I finally feel comfortable using it in my apps. There's a bit of an initial learning curve, but I think that is mostly a function of un-learning the LinearLayout/RelativeLayout state of mind. I've converted a number of my own layouts to use GridLayout and found them to be much faster/easier to understand afterwards.
There was one particular issue that I ran into that is worth noting, for its solution was not immediately obvious. I was making a two-column GridLayout, where an image was on the left and text was on the right. Here's a simplified version of this layout:
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Space android:layout_width="100dp" />
<TextView android:text="@string/two_cities" />
</GridLayout>
The two Views would align next to each other nicely, but the TextView on the right would end up overflowing off the edge (the example below is not cropped - this is how it appears on the device):
What was happening was that the TextView would take up the entire width of the GridLayout, instead of taking up just the width of the cell (as I expected).
It turns out the solution is to set the width of the TextView to zero, then let the View fill its space dynamically using layout_gravity:
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" > <Space android:layout_width="100dp" /> <TextView android:layout_width="0dip" android:layout_gravity="fill_horizontal" android:text="@string/two_cities" /> </GridLayout>
Now the problem is solved:
You need both of the highlighted attributes above; one or the other won't fix the problem. However, it will nicely solve the issue and should be kept in mind anytime you setup a GridLayout child to fill horizontally or vertically.