简体   繁体   English

Android TextView和文字换行

[英]Android TextView and text wrapping

I have a TextView inside a TableRow. 我在TableRow中有一个TextView。 The text contained in this TextView is tipically larger than the screen. 此TextView中包含的文本通常比屏幕大。 As such, I would like for the the text to wrap into multiple lines. 因此,我想将文本换成多行。

TableLayout table = (TableLayout)findViewById(R.id.myTable);
TableRow row = new Tablerow(context);
TextView view = new TextView(context);
view.setText(stringWithLotsOfCharacters);
view.setSingleLine(false);
row.add(view);
table.addview(row, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);

I tried using "setSingleLine" but it does not have the effect I was looking for. 我尝试使用“ setSingleLine”,但没有达到我想要的效果。

Any suggestions? 有什么建议么? Thanks. 谢谢。

Add android:shrinkColumns="1" for the column you want to wrap to the TableLayout in your layout or android:shrinkColumns="*" if you want to wrap all columns. 将要包装的列添加android:shrinkColumns="1"到布局中的TableLayout上 ;如果要包装所有列,请添加android:shrinkColumns="*"

You can also do it programmatically with setColumnShrinkable: 您也可以使用setColumnShrinkable以编程方式执行此操作:

public void setColumnShrinkable (int columnIndex, boolean isShrinkable)
view.setHeight(LayoutParams.WRAP_CONTENT);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM