2014-10-27 56 views
0

设置自动断线选项我在与它长文本 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.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); 

回答

1

你是给你的TextView所有它以 “WRAP_CONTENT” 希望空间的代码。确保你的父视图具有特定的宽度和尝试改变

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

txt.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT)); 
+0

上面的代码不工作 – sivanesan1 2014-10-27 11:49:38

+0

不工作怎么样?不编译或不给你期待的行为?因为我相信,代码编译,我怀疑你仍然没有得到在多行的字符串。在这种情况下,你需要确保父视图的宽度具有特定的宽度(即,它不是“WRAP_CONTENT”),或者设置一个特定的宽度在您tablerow的(例如,一套宽的“20dp”,而不是“MATCH_PARENT” ) – Fredrik 2014-10-27 12:13:36

相关问题