【问题标题】:Android and displaying multi-lined text in a TextView in a TableRowAndroid 并在 TableRow 的 TextView 中显示多行文本
【发布时间】:2011-07-10 23:01:27
【问题描述】:

我正在显示一个 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 换行到多行?

【问题讨论】:

  • A TableRow 就是这样 - 一行。我认为您不应该尝试在一行上创建多行,这就是重点。在那里使用 TableRow 的目的是什么?
  • 您是否尝试删除 android:singleLine="false" 行?
  • 刚刚在 Eclipse 中复制了您的代码,并且文本正确换行,正如我在图形布局中看到的那样。 (我刚刚添加了android:text="(a long text) " 来显示一些文字)
  • @All:谢谢你的帖子。如果有帮助,我将创建一个自定义 TableLayout 以在运行时显示可变数量的行。 @Adinia:我在运行时设置文本,可能就是这样。这可能是问题吗?
  • @All,尤其是。 Adinia:刚刚尝试在 XML 中设置一个很长的文本,而不是以编程方式,它没有换行 - 仍然在一行上。唔。不同的模拟器?不,不可能。

标签: android textview multiline tablelayout tablerow


【解决方案1】:

下面的代码也可以使用

<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>

【讨论】:

    【解决方案2】:

    如果文本所在的列设置为缩小,则 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>
    

    【讨论】:

    • 谢谢。我会在适当的时候尝试一下(我在这个项目上有一些事情要处理)并希望接受答案。
    • @All: android:shrinkColumns="0" 非常适合我,而不是我一直在尝试的android:shrinkColumns="1"。我不确定这是否是您的建议,但您的回答很可靠,我的评论出现在这里。他们一起回答了我的问题。
    • @All: android:stretchColumns="0" 似乎也有用/更好,如果(在我的情况下)表格只有一列您想拉伸以填充屏幕。我的 TableLayout XML 在此页面上的答案中给出(在键入时下方)。
    • 我知道为什么但是 android:stretchColumns 对我不起作用。但是 android:shrinkColumns 就像一个魅力。感谢您的回答和支持的cmets。
    • @SK9 答案是正确的,但有点草率。这里的目的是使带有文本的列缩小,因此应该在该参数中设置列的索引。我已经更新了答案,希望现在变得更清楚了。
    【解决方案3】:

    按代码也可以:

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

    1 是列数。

    【讨论】:

      【解决方案4】:

      为了完整起见,这是我的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 填充它。

      【讨论】:

        【解决方案5】:

        也许可以尝试如下:

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

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

        【讨论】:

          猜你喜欢
          • 2012-01-06
          • 1970-01-01
          • 2014-05-15
          • 1970-01-01
          • 2011-12-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-10-24
          相关资源
          最近更新 更多