如何在android textview中设置自动中断选项

如何在android textview中设置自动中断选项,android,textview,Android,Textview,我的android应用程序中有一个TextView,里面有长文本 问题是TextView不会自动断开到新行 这是我的代码,用于TextView TextView txt = new TextView(this); txt.setText("im not a superman and i have many friends not having any powers"); txt.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutP

我的android应用程序中有一个
TextView
,里面有长文本

问题是TextView不会自动断开到新行

这是我的代码,用于
TextView

TextView txt = new TextView(this);

txt.setText("im not a superman and i have many friends not having any powers");
txt.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
txt.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 13);
txt.setTextColor(Color.BLACK);          
txt.setPadding(dp(10), dp(10), dp(20), 0);
txt.setSingleLine(false);
txt.setLines(2);
txt.setMaxLines(4);
txt.setTypeface(Typeface.SERIF,Typeface.NORMAL);

您正在使用“包装内容”为TextView提供所需的所有空间。确保父视图具有特定的宽度,然后尝试更改

txt.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));


不工作怎么办?不编译或不提供您期望的行为?因为我确信代码会编译,所以我怀疑您仍然无法在多行上获取字符串。在这种情况下,您需要确保父视图的宽度具有特定的宽度(即,它不是“包裹内容”)或在tablerow上设置特定的宽度(例如,将宽度设置为“20dp”,而不是“匹配父视图”)
txt.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));