java将linearlayout动态添加到Relativelayout中
我有这段代码,我想在嵌套在RelativeLayout(RelativeLayout->;ScrollView->;LinearLayout->;我的复选框)中的ScrollView中的LinearLayout中动态添加复选框
li = (RelativeLayout) findViewById(R.id.mainlayout);
ScrollView sv = new ScrollView(this);
final LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
li.addView(sv);
sv.addView(ll);
for(int i = 0; i < 20; i++) {
CheckBox cb = new CheckBox(getApplicationContext());
cb.setText("I'm dynamic!");
ll.addView(cb);
}
this.setContentView(sv);
但我有一个错误:
03-12 20:32:14.840: E/AndroidRuntime(945): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
我的RelativeLayout已在XML文件中声明 我怎样才能解决这个问题
# 1 楼答案
检查这个http://developer.android.com/training/animation/screen-slide.html 下载示例应用程序时,请查看LayoutChangesActivity。爪哇
以下是添加项目的代码