0
  1. 我有一个 TableLayout,我需要在运行时添加 TableRows。
  2. 我需要为每个 TableRow 添加一个 ImageView 和一个 TextView,并将该行添加到 TableLayout。
  3. 如果文本的长度很长,添加到每一行的 TextView 应该多行显示。
  4. 我尝试了 setSingleLine(false)、setMaxWidth(100)、setMaxLines(3),但没有任何效果,但是如果我在 xml 中添加 TableRow 并使用 android:singleLine="false" 它会以多行显示文本。

请给我建议.......

通过此代码解决

TableRow.LayoutParams tlparams = new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT);
TextView textView = new TextView(this);
textView.setLayoutParams(tlparams);
textView.setText("New text: " + s);
textView.setSingleLine(false);
4

3 回答 3

2

下面的代码将为您工作。

TableRow.LayoutParams tlparams = new TableRow.LayoutParams( 
TableRow.LayoutParams.WRAP_CONTENT, 
TableRow.LayoutParams.WRAP_CONTENT); 
TextView textView = new TextView(this); 
textView.setLayoutParams(tlparams); 
textView.setText("New text: ");
textView.setMaxLines(3);
于 2012-10-11T06:48:58.520 回答
1

这是用于创建动态表

TableLayout TL=(TableLayout) findViewById(R.id.table);

TableRow tr=new TableRow(this);
tr.setLayoutParams(new LayoutParams( 
   LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));

TextView text_view=new TextView(this);
text_view.setWidth(100);// give how much you need
//set your text
tr.addView(text_view);

ImageView iv=new ImageView(this);
// set your image
tr.addView(iv);    

TL.addView(tr);

这是用于 TextView 中的新行。

String str="This is first line in textview1\nThis is\tsecond line in textview1";
text_view.setText(str);

或者

String str1 ="<font color=#00cc00>First line is green color in textview2</font><br/>"+
              " <font color=#ff0000>Second line is red color in textview2</font>";
text_view.setText(Html.fromHtml(str1));
于 2012-10-11T06:56:35.840 回答
0

有一个包含 TableLayout 的单独 xml 文件和一个包含 Table 行的单独 xml 文件,其中包含一个 ImageView 和一个 TextView

在运行时的代码中,使用 for 循环将表行添加到 tableLayout 中,如下所示

let table layout be as tl = (tableLayout)findViebyId(..);
for(int i =0;.....)
{
   final View child = getLayoutInflater().inflate(R.layout.inflateview, null);
    tl.addView(child);
}

这里的子视图是表格行 xml 文件的布局

于 2012-10-11T06:32:39.273 回答