这里是代码示例:
TableLayout ll = (TableLayout)findViewById(R.id.dyn_lyr);
也与LinearLayout相同
//LinearLayout ll = (LinearLayout)findViewById(R.id.dyn_lyr);
TextView tv1 = (TextView) findViewById(R.id.testEditText);
tv1.setText("SomeTextGoesHere");
for(int i=1 ; i<= 5 ; i++){
ll.addView(tv1);
}
LinearLayout / Table在ScrollView内部!
答案 0 :(得分:0)
您尝试添加的Child似乎已经有了父母。 你能分享一下logcat错误报告吗?
答案 1 :(得分:0)
没有足够的信息,但我猜Tv1已经有了父母。我想你应该每次都添加新的TextView。试试这个吧。好的,然后创建全新的TextView
for(int i=1 ; i<= 5 ; i++){
TextView tv1 = new TextView(this);
tv1.setText("SomeTextGoesHere");
ll.addView(tv1);
}
答案 2 :(得分:0)
您应该动态创建TextView并添加ll ...
for(int i=1 ; i<= 5 ; i++){
TextView tv1 = new TextView(getApplicationContext());
tv1.setText("SomeTextGoesHere");
ll.addView(tv1);
}
答案 3 :(得分:0)
如果您使用的是findViewById
,则生成的视图已经是您的布局的一部分,并且具有父级。每个视图只能添加到ViewGroup一次。您需要制作 new TextView并将其添加到LinearLayout。您可以通过TextView构造函数或带有单独xml布局的LayoutInflater来执行此操作。