Android中如何在TableRow的TextView中显示多行文本

51

我正在展示一个TableLayout,其中行如下:

<?xml version="1.0" encoding="utf-8"?>
<TableRow
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="wrap_content">

  <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

        <TextView   
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/one"
            android:layout_marginLeft="10dip"
            android:textColor="#B0171F" />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/one"
            android:id="@+id/two"
            android:layout_marginLeft="10dip"
            android:ellipsize="none"
            android:singleLine="false"
            android:scrollHorizontally="false"
            android:maxLines="10"
            android:textColor="@android:color/black" />

  </RelativeLayout>

</TableRow>
我已经尽我所能地查找和想到一切方法来使文本换行成多行,但却无济于事:文本总是被强制限制在一行上,超出了屏幕范围。也许这与我在一个TableRow内工作有关,据我所知,在此网站上还没有提及过这一点。
那么,我该如何强制我的第二个TextView进行换行呢?

一个TableRow就是一行。我认为你不应该试图在单个行上创建多行,这是它的重点。那么在那里使用TableRow的目的是什么? - Codemonkey
@所有人:感谢你们的帖子。如果有帮助的话,我正在创建一个自定义TableLayout来显示运行时的可变行数。@Adinia:我正在运行时设置文本,这可能是问题所在吗? - Ken
@所有人,尤其是Adinia:刚试了一下在XML中设置非常长的文本,而不是通过编程方式,但它没有换行-仍然在一行上。嗯。不同的模拟器?不可能。 - Ken
@Sheikh Aman:我在发布问题后添加了那行代码,尝试过加和不加。 - Ken
正如@Adinia所说,他/她在eclipse中尝试了您的项目,并且在图形布局编辑器中运行良好。我认为无论您是动态设置文本还是静态设置文本,都不会有影响,因为TextView将采用其设置的任何属性。您能否发布正在发生的屏幕截图?您使用的开发工具版本是什么? - Aman Alam
显示剩余3条评论
5个回答

99

如果包含TextView的列被设置为shrink,那么TextView会换行文本。有时它不会完全换行,如果文本以", ..."结尾,则比确切的n行略长。

以下是一个示例,具有id为question的TextView将自动换行:

<TableLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
    android:shrinkColumns="*">
    <TableRow>
         <TextView
             android:id="@+id/question"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"/>
    </TableRow>
</TableLayout>

谢谢您。我会在适当的时候尝试这个(我还有一些事情要处理这个项目),希望能够接受这个答案。 - Ken
12
@所有人:对于我来说,android:shrinkColumns="0" 的效果非常好,而不是我之前一直尝试的 android:shrinkColumns="1"。我不确定这是否是你们所建议的,但是你们的答案很有力,我的评论也出现在这里。两者共同回答了我的问题。 - Ken
@所有人:android:stretchColumns="0" 在某些情况下也很有用,比如说你的表格只有一列需要填满整个屏幕时。我的TableLayout XML代码在本页面下方(在我写这篇翻译时)给出了答案。 - Ken
关于 stretch/shrinkColumns="???" 的疑惑,你说得很对。大多数情况下,我使用 android:shrinkColumns="*"。对于一些特殊的情况,我会显式地使用要被拉伸/收缩的列号码。 - Michael Biermann
1
我不知道为什么android:stretchColumns对我无效。但是android:shrinkColumns非常好用。感谢您的答案和支持性评论。 - Ahmed Faisal
2
@SK9 答案是正确的,但有些粗糙。这里的目的是使文本所在的列缩小,因此应该在该参数中设置该列的索引。我已经更新了答案,希望现在更清晰了。 - Malcolm

15

为了完整起见,这是我的TableLayout在XML中的读法:

<TableLayout
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_marginTop="10dp"
  android:shrinkColumns="0"
  android:stretchColumns="0"
  android:id="@+id/summaryTable">
</TableLayout>

稍后我将使用TableRow填充这个内容。


3

代码也可以实现:

TableLayout tablelayout = (TableLayout) view.findViewById(R.id.table);
tablelayout.setColumnShrinkable(1,true);

1 是列的编号。


2
也许可以尝试以下方法:
myTextView.setText(Html.fromHtml("On " + stringData1 + "<br> at " + strinData2);

我知道这看起来有点像“轮椅”解决方案,但对我来说很有效,尽管我也通过程序填写文本。


0

您也可以使用以下代码

<TableRow >
    <TextView
        android:id="@+id/tv_description_heading"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="left"
        android:padding="8dp"
        android:text="@string/rating_review"
        android:textColor="@color/black"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/tv_description"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="left"
        android:maxLines="4"
        android:padding="8dp"
        android:text="The food test was very good."
        android:textColor="@color/black"
        android:textColorHint="@color/hint_text_color" />
</TableRow>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接