Reputation: 201
I am using TableLayout within a ListView. The data I'm presenting is tabular in nature and the TableLayout seems like a good way to ensure that the columns line up as desired. This approach worked well most of the time - see below.
But occasionally the Views representing the columns wrapped their text content as shown below.
After searching around for a while, I came across the following discussion on the Android Google Group. In response, I set android:stretchColumns="*"
in the TableLayout and voila, the contained TextViews stopped mysteriously wrapping their text.
Here's a partial content of the layout's XML, with the change highlighted with a comment:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="*"> <!-- stretchColumns needed to prevent
TableLayout from unilaterally deciding to shrink the column
width and thereby causing text to wrap. -->
While I'm now getting the desired result, I'm a bit confused as to why this "fixed" my problem. The TableLayout documentation states that "The width of a column is defined by the row with the widest cell in that column." But if that's true, why did the columns apparently shrink in size in the first place causing the text to wrap?
Does anyone have a good explanation?
Upvotes: 1
Views: 3023
Reputation: 36836
Containers query their children for how much space they want, and this process continues recursively. Any dynamically sized layout framework is going to work this way. From the inside out, children report up to their parents their desired size, and then on a second pass the parents report back to their children how much space is available to them. When they're not included in the stretchColumns list (either explicitly by position, or implicitly by wildcard as in the example), they ask for the bare minimum which in the case of TextView is the minimum space required to display the text wrapped. When they're included in the stretchColumns list, they report the width required to display the entire contents, and if it fits they don't get wrapped. Anyone who requests more space than is available to them gets wrapped text. I imagine the priority is given in order from least space required up to the most so if you have stretchColumns="*"
the largest blocks of text are getting wrapped first (if necessary) and everyone else gets to be displayed normally.
Upvotes: 1