【发布时间】:2011-10-28 14:30:39
【问题描述】:
我尝试让 ScrollView 像 ListView 一样工作。 每一行都将是一个动态添加的 TextView。 所以我试试这段代码
this.scrollView = (ScrollView) findViewById(R.id.scrollView1);
this.linearLayout = (LinearLayout) findViewById(R.id.linearLayout2);
this.linearLayout.setOrientation(LinearLayout.VERTICAL);
TextView[] tx = new TextView[10];
for (int i = 0; i < 10; i++) {
tx[i] = new TextView(this);
tx[i].setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
tx[i].setText("This is the textviewNo" + i);
this.linearLayout.addView(tx[i]);
}
this.scrollView.addView(this.linearLayout);
setContentView(this.scrollView);
但我得到了这个例外
10-28 13:35:59.120: ERROR/AndroidRuntime(2346): Caused by: java.lang.IllegalStateException: ScrollView can host only one direct child
在 this.scrollView.addView(this.linearLayout) 这一行 我做错了什么? 谢谢
【问题讨论】:
标签: android dynamic textview scrollview