Android and displaying multi-lined text in a TextView in a TableRow

ⅰ亾dé卋堺 提交于 2019-11-28 04:50:33

The TextView will wrap the text if the column it's in is set to shrink. Sometimes it does not wrap exactly, if the text ends with ", ...", then it is a little bit longer than exactly n lines.

Here's an example, the TextView with the id question will wrap:

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

For the sake of completeness, this is how my TableLayout reads in 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>

I later populate this with TableRows.

By code also works:

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

1 is the number of column.

Maybe try as follows:

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

I know, looks a bit like a "wheelchair" solution, but works for me, though I also fill a text programmatically.

You can also used below code

<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>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!