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 换行到多行?

4

5 回答 5

99

如果文本所在的列设置为缩小,则 TextView 将包装文本。有时它不完全换行,如果文本以“,...”结尾,那么它比正好 n 行长一点。

这是一个示例,带有 id 的 TextViewquestion将换行:

<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>
于 2011-03-21T22:04:42.060 回答
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>

我稍后用TableRows 填充它。

于 2011-03-23T04:32:35.447 回答
3

通过代码也可以:

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

1 是列数。

于 2014-09-10T11:08:28.567 回答
2

也许尝试如下:

myTextView.setText(Html.fromHtml("On " + stringData1 + "<br> at " + strinData2);

我知道,看起来有点像“轮椅”解决方案,但对我有用,虽然我也以编程方式填写文本。

于 2011-03-21T15:44:54.470 回答
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>
于 2018-04-27T10:15:17.447 回答