动态添加的TableRow到TableLayout [英] Dynamically add TableRow to TableLayout
问题描述
当点击一个按钮,下面的方法是运行:
When a button is clicked, the following method is run:
public void createTableRow(View v) {
TableLayout tl = (TableLayout) findViewById(R.id.spreadsheet);
TableRow tr = new TableRow(this);
LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
tr.setLayoutParams(lp);
TextView tvLeft = new TextView(this);
tvLeft.setLayoutParams(lp);
tvLeft.setBackgroundColor(Color.WHITE);
tvLeft.setText("OMG");
TextView tvCenter = new TextView(this);
tvCenter.setLayoutParams(lp);
tvCenter.setBackgroundColor(Color.WHITE);
tvCenter.setText("It");
TextView tvRight = new TextView(this);
tvRight.setLayoutParams(lp);
tvRight.setBackgroundColor(Color.WHITE);
tvRight.setText("WORKED!!!");
tr.addView(tvLeft);
tr.addView(tvCenter);
tr.addView(tvRight);
tl.addView(tr, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
}
R.id.s preadsheet
是一个XML TableLayout。我可以从正在访问的方法调试见,但没有被绘制到屏幕上。怎么办?我是否需要以某种方式重新查看内容?
R.id.spreadsheet
is an xml TableLayout. I can see from debugging that the method is being accessed, but nothing is drawn to the screen. What gives? Do I need to reset the Content View somehow?
推荐答案
事实证明,Eclipse是并不总是正确的。 Ctrl + Shift + M做出了错误的进口。它有进口android.view.ViewGroup.LayoutParams
当它应该有进口android.widget.TableRow.LayoutParams
As it turns out, Eclipse is not always right. Ctrl+Shift+M made the wrong import. It had import android.view.ViewGroup.LayoutParams
when it should have import android.widget.TableRow.LayoutParams
现在一切工作正常。
这篇关于动态添加的TableRow到TableLayout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!