Android TableLayout中的ListView,神秘的列缩小
我在 ListView 中使用 TableLayout。我呈现的数据本质上是表格形式的,而 TableLayout 似乎是确保列按需要排列的好方法。这种方法在大多数情况下都很有效 - 见下文。
但有时代表列的视图会包裹其文本内容,如下所示。
搜索了一段时间后,我发现了以下内容Android Google 群组上的讨论。作为响应,我在 TableLayout 中设置了 android:stretchColumns="*"
,瞧,包含的 TextView 停止了神秘地换行其文本。
下面是布局 XML 的部分内容,其中的更改用注释突出显示:
<?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. -->
虽然我现在得到了所需的结果,但我有点困惑为什么这“解决”了我的问题。 TableLayout 文档 指出“列的宽度由该列中单元格最宽的行。”但如果这是真的,为什么列的大小首先明显缩小,导致文本换行?
有人有好的解释吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
容器查询其子级需要多少空间,并且此过程递归地继续。任何动态大小的布局框架都将以这种方式工作。从内到外,孩子们向父母报告他们想要的尺寸,然后在第二次传递时,父母向他们的孩子报告他们可以使用多少空间。当它们不包含在stretchColumns列表中时(无论是显式地按位置,还是隐式地通过通配符,如示例中所示),它们会要求最小空间,在TextView的情况下是显示换行文本所需的最小空间。当它们包含在stretchColumns列表中时,它们会报告显示整个内容所需的宽度,如果适合,则不会被换行。任何请求比可用空间更多的空间的人都会收到换行文本。我想优先级是按所需空间从最小到最大的顺序给出的,因此如果您有
stretchColumns="*"
,则最大的文本块将首先被换行(如果需要),而其他人则可以可以正常显示。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.